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

extend remote schema input type error #1177

Closed
riskgod opened this issue Jul 25, 2019 · 8 comments
Closed

extend remote schema input type error #1177

riskgod opened this issue Jul 25, 2019 · 8 comments

Comments

@riskgod
Copy link

riskgod commented Jul 25, 2019

my remote schema:

  type Post {
    postId: ID!
    postTitle: String!
    postContent: String!
    postAuthorId: ID
  }
  input PostTag {
    name: String!
  }
  input PostInput {
    postTitle: String!
    postContent: String!
    postAuthorId: ID!
    postTags: [PostTag!]!
  }
  type CommonResponse {
    code: Int!
    message: String!
  }
  type Query {
    posts: [Post]!
  }
  type Mutation {
    addPost(post: PostInput): CommonResponse!
  }

My extend schema

extend input PostTag {
    color: String
  }

this is the demo code:

https://github.com/mrdulin/apollo-graphql-tutorial/tree/master/src/extend-remote-schema

on my playgroud I can see my schema but when I call this the error is :

"Variable "$_v0_input" got invalid value "

Anyone can help this?

@yaacovCR
Copy link
Collaborator

yaacovCR commented Jul 25, 2019

Currently, mergeSchemas filters on delegation to remove output type fields added to selection set by extend, but does not filter out fields added by extend on input types.

Workaround is to manually create a delegating resolver from your root field to the original field and filter the args parameter on your own.

This should not be too much extra work as presumably you would have a manual delegating resolver for the root field already to take an advantage of whatever input field you added on extend.

@riskgod
Copy link
Author

riskgod commented Jul 25, 2019

Currently, mergeSchemas filters on delegation to remove output type fields added to selection set by extend, but does not filter out fields added by extend on input types.

Workaround is to manually create a delegating resolver from your root field to the original field and filter the args parameter on your own.

This should not be too much extra work as presumably you would have a manual delegating resolver for the root field already to take an advantage of whatever input field you added on extend.

sorry. would u mind share more details? Thanks

@yaacovCR
Copy link
Collaborator

Sure. Why don't you start by describing what you are trying to accomplish by extending an input field.

@riskgod
Copy link
Author

riskgod commented Jul 26, 2019

Sure. Why don't you start by describing what you are trying to accomplish by extending an input field.

Yes, I need to add a field in PostInput, so I can get this to save on my local database

@yaacovCR
Copy link
Collaborator

It sounds like what you are trying to do is delegate your local addPost mutation to the remote schema, but also save additional metadata about the post to your local database.

mergeSchemas automatically provides for you a resolver that will delegate your local addPost mutation to the remote:

It looks like this (see https://github.com/apollographql/graphql-tools/blob/master/src/stitching/mergeSchemas.ts#L392)

(root, args, context, info) => {
    return info.mergeInfo.delegateToSchema({
      schema,
      operation,
      fieldName,
      args,
      context,
      info,
    });
  };

If you want a root field like addPost to do something more than just simple delegating, you will have to override the above resolver with your own in your call to mergeSchemas.

So what looks like this (https://github.com/mrdulin/apollo-graphql-tutorial/blob/master/src/extend-remote-schema/service-2/server.ts#L15)

const schema = mergeSchemas({ schemas: [remoteExecutableSchema, typeDefs] });

should end up looking something like this:

const schema = mergeSchemas({
  schemas: [remoteExecutableSchema, typeDefs]
  resolvers: {
    Mutation: {
      addPost: (root, args, context, info) => {
        const result = info.mergeInfo.delegateToSchema({
          schema,
          operation,
          fieldName,
          args,
          context,
          info,
        });
        if (result.code !== ERROR_CODE) {
          saveLocalData(post.title, post.tags);
          return result;
        }
      };
    }
  }
});

This will still fail. At this point, delegateToSchemas is not smart enough to strip out the fields you add to your input types prior to delegation. But the workaround is to do that manually:

const schema = mergeSchemas({
  schemas: [remoteExecutableSchema, typeDefs]
  resolvers: {
    Mutation: {
      addPost: (root, args, context, info) => {
        const result = info.mergeInfo.delegateToSchema({
          schema,
          operation,
          fieldName,
          args: { post: mapPostInput(args.post) },  //   <== changed line
          context,
          info,
        });
        if (result.code !== ERROR_CODE) {
          saveLocalData(post.title, post.tags);
          return result;
        }
      };
    }
  }
});

where you also define a new function:

function mapPostInput(post) {
  return {
    ...post
    tags: post.tags.map( tag => {
      const { ...rest, color } = tag;
      return rest;
    });
  }
}

@yaacovCR
Copy link
Collaborator

Actually, I am not sure this is the issue. Will have to try example above. What exact query are you sending with that variable value?

@yaacovCR yaacovCR changed the title extend remote schema error extend remote schema input type error Mar 29, 2020
@yaacovCR
Copy link
Collaborator

I think the problem is that the other fields in postInput are required. Closing for now, feel free to reopen as necessary.

@yaacovCR
Copy link
Collaborator

See #1058 and #1306 for possible solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants