Skip to content

[with graphql-codegen] Using optional argument, but required to pass argument #10

@mackeee-orange

Description

@mackeee-orange

Hi @Quramy .
I love this library. This is very useful.

I am using it with graphql-codegen.

define fragment

export const KCG_CARD_FRAGMENT = gql`
  fragment KCGCardKnowledgeCategoryGroup on KnowledgeCategoryGroup
  @argumentDefinitions(
    level: { type: "EngineerLevel", defaultValue: NEWBIE }
    occupation: { type: "EngineerOccupation", defaultValue: BACKEND }
  ) {
    id
    name
    programmingResource {
      id
      name
      thumbnailUrl
    }
    knowledgeCategories(level: $level, occupation: $occupation) {
      edges {
        node {

and using the fragment like this

export const CREATE_KCG_MUTATION = gql`
  mutation CreateKnowledgeCategoryGroup($name: String!, $programmingResourceId: ID) {
    createKnowledgeCategoryGroup(
      input: { name: $name, programmingResourceId: $programmingResourceId }
    ) {
      knowledgeCategoryGroup {
        ...KCGCardKnowledgeCategoryGroup
      }
    }
  }

  ${KCG_CARD_FRAGMENT}
`;

Where, those arguments level and occupation are optional.
But graphql-codegen says

AggregateError: 
        GraphQLDocumentError: Variable "$level" is not defined by operation "CreateKnowledgeCategoryGroup".

Does it bug??
If it is my fault, please teach me correct definition.

version info

node is v14.15.4

    "graphql": "14.7.0",
    "@graphql-codegen/add": "2.0.1",
    "@graphql-codegen/cli": "1.16.3",
    "@graphql-codegen/typescript": "1.16.3",
    "@graphql-codegen/typescript-operations": "1.16.3",
    "@graphql-codegen/typescript-react-apollo": "1.16.3",
    "@graphql-codegen/fragment-matcher": "1.17.8",
    "apollo-link-fragment-argument": "1.0.1",

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions