Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

transformSchema: remove input fields by name. #1058

Closed
runar-indico opened this issue Feb 3, 2019 · 11 comments
Closed

transformSchema: remove input fields by name. #1058

runar-indico opened this issue Feb 3, 2019 · 11 comments
Labels
docs Focuses on documentation changes

Comments

@runar-indico
Copy link

In my current schema, I have some about 15-ish types like this:

type CoffeShop {
    name: String!
    createdBy: User!
   ...
}

all of them having a createdBy-reference to the user. The complete schema is then generated, with queries, mutations and all. However, we don't want users to be able to specify createdBy when they are doing mutaitons, as we get that information from authentication-information.

We have tried to remove overy instance of createdBy from the generated input-types, but this is fast turning into a chore.

After looking at transformSchema, we see that it could possibly be used for this. However, I find it a bit difficult to know exactly how I should transform. I could not find this information in the documentation. What fields in the schema would be input?

Basically, I'd like to remove every field of type input that has the name createdBy.

@ravikanthreddy89
Copy link

+1, Tried FilterByTypes and FilterRootFields couldn't make it work.

@runar-indico
Copy link
Author

As a temporary workaround, I created a rough schema-generator for this. It is very basic, but we have used it successfully for about a month now, doing most of what we need in a very dynamic way. It is of course not an ideal solution. https://github.com/runar-indico/graphql-schema-transformer

This was referenced Mar 31, 2020
@yaacovCR
Copy link
Collaborator

yaacovCR commented Apr 3, 2020

Moving to v5.1, reopening to move any relevant discussion here.

@yaacovCR yaacovCR reopened this Apr 3, 2020
@yaacovCR
Copy link
Collaborator

yaacovCR commented May 5, 2020

This is not as simple as changing the input objects, because you also have to provide a mechanism for setting default values for non optional input values

@alanwillms
Copy link

@yaacovCR Should I open a separate issue to deal with query and mutation arguments?

@yaacovCR
Copy link
Collaborator

I think it would all be part of same transformer, so can be tracked together

@yaacovCR
Copy link
Collaborator

See #1551 which allows filtering and renaming input fields but does not yet address fully the issue of modifying the arguments.

Please comment there on desired approach.

@yaacovCR
Copy link
Collaborator

yaacovCR commented Jun 4, 2020

#1551 has been improved to allow setting default values for filtered input fields at schema generation time or at delegation time..

@yaacovCR
Copy link
Collaborator

yaacovCR commented Jun 4, 2020

Try out the new FilterInputObjectFields transform from #1551:

npx match-version @graphql-tools 6.0.8-alpha-4aeb995.0

Usage as follows:

  describe('transform input object fields', () => {
    test('filtering works', async () => {
      const schema = makeExecutableSchema({
        typeDefs: `
          input InputObject {
            field1: String
            field2: String
          }

          type OutputObject {
            field1: String
            field2: String
          }

          type Query {
            test(argument: InputObject): OutputObject
          }
        `,
        resolvers: {
          Query: {
            test: (_root, args) => {
              return args.argument;
            }
          }
        }
      });

      const transformedSchema = wrapSchema(schema, [
        new FilterInputObjectFields(
          (typeName, fieldName) => (typeName !== 'InputObject' || fieldName !== 'field2'),

          // Below is where you can supply the targetSchema with a value for the
          // stripped input field -- if you want

          // delegationContext gives you access to the overall delegationContext
          // i.e. the relevant options from delegateToSchema
          // with the graphql context under delegationContext.context, for example

          // request gives you access to the request so far, including variables, etc.

          (typeName, inputObjectNode, /* delegationContext, request */) => {

            if (typeName === 'InputObject') {
              return {
                ...inputObjectNode,
                fields: [...inputObjectNode.fields, {
                  kind: Kind.OBJECT_FIELD,
                  name: {
                    kind: Kind.NAME,
                    value: 'field2',
                  },
                  value: astFromValue('field2', GraphQLString),
                }],
              };
            }
          }
        )
      ]);

      const query = `{
        test(argument: {
          field1: "field1"
        }) {
          field1
          field2
        }
      }`;

      const result = await graphql(transformedSchema, query);
      expect(result.data.test.field1).toBe('field1');
      expect(result.data.test.field2).toBe('field2');
    });
  });

@yaacovCR
Copy link
Collaborator

yaacovCR commented Jun 9, 2020

Should be available in latest release, needs docs

@yaacovCR yaacovCR added the docs Focuses on documentation changes label Jun 9, 2020
@ardatan
Copy link
Owner

ardatan commented Apr 14, 2021

Let's close this for now because we already need work for most of transforms :)

@ardatan ardatan closed this as completed Apr 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Focuses on documentation changes
Projects
None yet
Development

No branches or pull requests

5 participants