Graphql-tools: TransformRootField only works for default root type names

Created on 4 Jun 2020  路  7Comments  路  Source: ardatan/graphql-tools

In the code below my local schema CloudPDF does rename the types and also rename the root fields but my remote schema TakeShape it does rename the types but doesn't rename the root fields

I created an API key for this issue on TakeShape so the below remote schema should work.

  const executor = async ({ document, variables }: any) => {
    const query = print(document);
    const fetchResult = await fetch(`https://api.takeshape.io/project/3fe47d2c-491b-421b-b683-e13a72a455cc/graphql`, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'Authorization': 'Bearer 53bbb42644e245fc8089857448d3819d'
      },
      body: JSON.stringify({ query, variables })
    });
    return fetchResult.json();
  };

  const takeShapeTransforms = [
    new RenameTypes((name: string) => `TakeShape_${name}`),
    new RenameRootFields((_operation: string, name: string) => `TakeShape_${name}`)
  ];

  const cloudPdfTransforms = [
    new RenameTypes((name: string) => `CloudPDF_${name}`),
    new RenameRootFields((_operation: string, name: string) => `CloudPDF_${name}`)
  ];

  const takeShapeSchema = await introspectSchema(executor);
  const cloudPdfSchema = await createSchema();

  const schema = stitchSchemas({
    subschemas: [
      { schema: takeShapeSchema, executor, transforms: takeShapeTransforms },
      { schema: cloudPdfSchema, transforms: cloudPdfTransforms }
    ]
  })

Most helpful comment

Fix: Transformer should be set up with actual root type name in transformSchema method rather than in constructor

https://github.com/ardatan/graphql-tools/blob/master/packages/wrap/src/transforms/TransformRootFields.ts#L17

All 7 comments

Bug can be isolated to even local schemas, if they don't have the default root type names. TakeShape, for example, uses Root instead of Query.

For whatever reason, I created a RenameRootTypes transform that can change the root type name (https://github.com/ardatan/graphql-tools/blob/ae7c968deb2e6f51024a385abfc6455c0db5a5df/packages/wrap/src/transforms/RenameRootTypes.ts), but did not make it so that this step is not required.

This may be because merging of root types now works similarly to how other types can be merged without special logic -- I am not sure if this is that way it used to be in v4, so this could be a regression, but the RenameRootTypes transform is available as a workaround.

Fix could be to add this transform automatically, but after thinking about it, I am again not sure if the best thing would be to do so or to keep the current behavior...

Thanks @yaacovCR the RenameRootTypes workaround is working for now

Not sure if that's the same problem I have right now. When my query type is not called Query and my mutation type is not called Mutation I can't rename root fields:

schema {
  query: EstablishmentQuery
  mutation: EstablishmentMutation
}

type EstablishmentQuery {
  # Cannot rename this via RenameRootFields:
  info: AppInfo
}

I will try your solution.

Fix: Transformer should be set up with actual root type name in transformSchema method rather than in constructor

https://github.com/ardatan/graphql-tools/blob/master/packages/wrap/src/transforms/TransformRootFields.ts#L17

Should be fixed in 6.0.9. Let us know if anything! Thanks so much!

Was this page helpful?
0 / 5 - 0 ratings