简体   繁体   中英

Syntax for submitting a mutation to a graphql-relay mutationWithClientMutationId

I defined a GraphQL Mutation using graphql-relay but am having issues figuring out how to submit a mutation to it.

Here is the relevant schema:

const userType = new GraphQLObjectType({
  name: 'User',
  description: 'user',
  fields: () => ({
    id: {
      type: new GraphQLNonNull(GraphQLString),
      description: 'The UUID for the user.',
      resolve(user) {
        return user.uuid;
      },
    },
  })
});

const registerUser = mutationWithClientMutationId({
  name: 'registerUser',
  inputFields: {
  },
  outputFields: {
    user: {
      type: userType,
      resolve: (payload) => {
        models.user.findById(payload.userId);
      }
    },
  },
  mutateAndGetPayload: (args) => {
    var newUser = models.user.build().save();
    return {
      userId: newUser.id,
    };
  }
});


const rootMutation = new GraphQLObjectType({
  name: 'RootMutationType',
  fields: {
    registerUser: registerUser,
  },
});

const schema = new GraphQLSchema({
  query: rootQuery,
  mutation: rootMutation,
});

What should an HTTP call look like to register a new user and get back the userId?

Thanks!

I want to point out that I see that you're saying that your mutation requires no parameters - how does it know what the new user's details are? You'll probably need some parameters on that mutation, eventually. They would be available to your mutateAndGetPayload on that first function parameter. (I'm not saying every mutation needs parameters, but this one probably does)

If you're using Relay, there is some pretty good information on the official document as to how to use your mutations from Relay. Particularly down at the bottom where it shows the various mutator configs. If you're using connections, you may want to use RANGE_ADD to add this new account to the Relay store manually, otherwise if you'd like to perform a more broad refetch you can use FIELDS_CHANGE . You said you need the new user id after the mutation finishes. If you're using Relay, you may need to look into REQUIRED_CHILDREN to specify that regardless of the computed query that Relay will build, you always want that id to be queried.

The output of your mutation is a userType , so you'd be able to access it with a fragment on the payload type, which would probably be RegisterUserPayload , that might look something like ...

fragment on RegisterUserPayload {
  user {
    id
  }
}

Now, that's assuming you're using Relay. If you'd like to try this out manually via GraphiQL, then you can use the examples of how to do mutations through there on the GraphQL Mutation docs . There's a direct example of how you'd query your mutation.

Last, since you asked how to do this at a low level of issuing the HTTP request yourself, for that you can look at express-graphql documentation, which explains how to query it.

I figured out a mutation format that worked:

mutation RootMutationType {
  registerUser(input:{clientMutationId:"123"}){
    clientMutationId, user { id }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM