Nest a query inside mutation graphql

ぐ巨炮叔叔 提交于 2019-12-11 19:04:36

问题


As you need to include a query inside of your mutation to get the internal cache to update after a mutation I have now come across a situation where I have a query with a function that accepts variables. I cannot find a way to include this query inside the mutation.

I thought I may need to pass all variables inside the mutation including those that are needed for the query, but this makes no sense.

Works correctly, this will redraw once the mutation has finished

export const GET_PICK_TICKETS = gql`
  {
    allPickTickets {
      edges {
        node {
          id
          createdBy
          createdAt
          status
        }
      }
    }
  }
`

export const MARK_PICK_TICKET_COMPELTE = gql`
  mutation markPickTicketAsCompleted($id: Int!) {
    updatePickTicketById(input: { id: $id, pickTicketPatch: { status: "PICKED" } }) {
      clientMutationId
      query ${GET_PICK_TICKETS}
    }
  }
`

Does not work

When using a query function it will fail

  mutation updateMemberTaskById($id: Int!, $startedAt: Datetime!) {
    updateMemberTaskById(input: { id: $id, memberTaskPatch: { startedAt: $startedAt, status: "STARTED" } }) {
      clientMutationId
      query getLocationSet($locationSetId: Int!, $internalId: Int, $jobcardIds: [String!]) {
        locationSet: locationSetById(id: $locationSetId) {
          id
          set: setBySetId {
            id
            name
          }
        }
      }
    }
  }
 * please ignore that the variables are not being used in the query.

error produced from graphql formatter:

Syntax error while parsing GraphQL query. Invalid input ""STARTED" } }) {\n      clientMutationId\n      query getLocationSet($", expected Argument, SelectionSet, Value, Directives or ObjectField (line 2, column 94):
    updateMemberTaskById(input: { id: $id, memberTaskPatch: { startedAt: $startedAt, status: "STARTED" } }) {                                                                                                 
                                                                                             ^

Maybe I do not need to do a query, but according to this answer I do.

来源:https://stackoverflow.com/questions/57205665/nest-a-query-inside-mutation-graphql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!