remorses / genql

Type safe TypeScript client for any GraphQL API
https://genql.dev
MIT License
881 stars 37 forks source link

Document example for generateMutationOp #169

Closed jeroenmaas closed 7 months ago

jeroenmaas commented 7 months ago

First of all this is a great library!

I'm having trouble integration the generation of mutations with apollo. On the documentation it refers to generateMutationOp (https://genql.dev/docs/usage/integrate-with-other-graphql-clients) but the variables seem to have a weird indexing. v1, v2 etc. I found multiple issues references to this but I haven't been able to decrypt how it is suppose to work. It would be great of this could be documented with an example. I'm currently using the following workaround:

   const mutationSelection = {
      createOrUpdateObject: {
        __args: {
          input: {
            id: '1',
            ...data
          }
        },
        object: {
          id: true,
          field1: true,
          field2: true
        },
        errors: {
          field: true,
          message: true
        }
      }
    };

    type f = FieldsSelection<Mutation, typeof mutationSelection>;
    const {query, variables } = generateMutationOp(mutationSelection);
    return this.apollo.mutate<f>({
      mutation: gqltag(query),
      variables: variables ? {'input': Object.values(variables)[0]} : {}
    });

Found some related issues but they have not been able to help me further.

https://github.com/remorses/genql/issues/143 https://github.com/remorses/genql/issues/130

jeroenmaas commented 7 months ago

Ah my bad. I was testing out something else that broke it. Passing variables variables directly to apollo works as expected...

Just leaving my code example for anyone that might have the same issue in the future:

const mutationSelection = {
  createOrUpdateObject: {
    __args: {
      input: {
        id: '1',
        ...data
      }
    },
    object: {
      id: true,
      field1: true,
      field2: true
    },
    errors: {
      field: true,
      message: true
    }
  }
};

type f = FieldsSelection<Mutation, typeof mutationSelection>;
const {query, variables } = generateMutationOp(mutationSelection);
return this.apollo.mutate<f>({
  mutation: gqltag(query),
  variables: variables
});