简体   繁体   中英

GraphQL query structure

I'm learning GraphQL and am working with Apollo (client and server) I still have some confusion with the structure of GraphQL queries.

Questions:

  1. Is it possible to merge the lines labeled #1 and #2 together? As the lines are so similar it almost feels redundant to have both.
  2. If it is possible, is it advisable?
  3. If not, what is the benefit of having a query structured in this way?
const AddUserQuery = gql`
  /*#1*/mutation addUser($firstName: String!, $lastName: String!, $email: String!) {
    /*#2*/addUser(firstName: $firstName, lastName: $lastName, email: $email) {
      id,
      firstName,
      lastName,
      email
    }
  }`;
...
const [addUser, {data}] = useMutation(AddUserQuery);

Thanks in advance,

No. Basically #1 is you, telling Apollo you want a mutation, it's up to you if you name it or not, then, in #2 , you're telling Apollo, which mutation you want to use.

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