zino-hofmann / graphql-flutter

A GraphQL client for Flutter, bringing all the features from a modern GraphQL client to one easy to use package.
https://zino-hofmann.github.io/graphql-flutter
MIT License
3.25k stars 620 forks source link

How to Work with Custom Data types #1152

Closed ShivamMattoo33 closed 2 years ago

ShivamMattoo33 commented 2 years ago

Hello I am new to GraphQl and flutter and I have a question and I put this under enhancement because I dont see any documentation for it or any examples on how to do this so not sure if it exists

I have this query

mutation UpdateJobStatus {
  updateJobStatus(input: { 
    job: { 
       id: "5du2ZM84qIJmwR9rG7CFgH", 
       status: Pending  **// This Thing here has a custom type**
}}) {
    job {
      id
      status
      coverage {
          result
      }
    }
  }
}

The status here is not a primitive data type but of type JobStatusID which can take different values

Now In flutter/dart I have this

String updateJobStatusPending = r'''
mutation UpdateJobStatus($jobId: ID!, $status: JobStatusId! ) {
  updateJobStatus(input: { 
    job: { 
       id: $jobId, 
       status:$status 
}}) {
    job {
      id
      status
      coverage {
          result
      }
    }
  }
}

My Question is : How do i pass this in variable here

 final MutationOptions options = MutationOptions(
      document: gql(updateJobStatusPending),
      variables: <String, String>{
        'jobId': jobId,
        'status':  **// What do I pass here as its not a primitive data type and I cant just type Pending here with ""** 
      },
budde377 commented 2 years ago

I'm assuming that the JobStatusID is an enum. In that case, you can just pass the string value that you want, e.g. "Pending". The same goes for scalars, but they can take any primitive data type and it's up to the server to pass them correctly.

So in the end you're always limited to json data types, arrays, objects, primitives, which are lists, maps, and appropriate primitives in dart.

I hope this makes sense.

ShivamMattoo33 commented 2 years ago

Passing a string “pending” doesn’t work it needs Pending without string

budde377 commented 2 years ago

Try and write the string "Pending" and see if it works.

ShivamMattoo33 commented 2 years ago

It does not work. Also FYI we are working on a 3rd party API and just consuming it. Attaching the exception

{
    "errors": [
        {
            "message": "Argument 'status' on InputObject 'UpdateJobStatusJobInput' has an invalid value (\"Pending\"). Expected type 'JobStatus!'.",
            "locations": [
                {
                    "line": 5,
                    "column": 10
                }
            ],
            "path": [
                "mutation UpdateJobStatus",
                "updateJobStatus",
                "input",
                "job",
                "status"
            ],
            "extensions": {
                "code": "argumentLiteralsIncompatible",
                "typeName": "InputObject",
                "argumentName": "status"
            }
        }
    ]
}
Here is the request I typed
mutation UpdateJobStatus {
  updateJobStatus(input: { 
    job: { 
       id: "5du2ZM84qIJmwR9rG7CFgH", 
       status: "Pending" }}) {
    job {
      id
      status
      coverage {
          result
      }
    }
  }
budde377 commented 2 years ago

Okay, but the example you're posting here is not what you asked about.

In

mutation UpdateJobStatus {
  updateJobStatus(input: { 
    job: { 
       id: "5du2ZM84qIJmwR9rG7CFgH", 
       status: "Pending" }}) {
    job {
      id
      status
      coverage {
          result
      }
    }
  }

You don't have any variables, so it should be

mutation UpdateJobStatus {
  updateJobStatus(input: { 
    job: { 
       id: "5du2ZM84qIJmwR9rG7CFgH", 
       status: Pending }}) {
    job {
      id
      status
      coverage {
          result
      }
    }
  }

in your original question, you had the query:

String updateJobStatusPending = r'''
mutation UpdateJobStatus($jobId: ID!, $status: JobStatusId! ) {
  updateJobStatus(input: { 
    job: { 
       id: $jobId, 
       status:$status 
}}) {
    job {
      id
      status
      coverage {
          result
      }
    }
  }
}

then the variables should be

 final MutationOptions options = MutationOptions(
      document: gql(updateJobStatusPending),
      variables: <String, String>{
        'jobId': jobId,
        'status':  "Pending"
      },
ShivamMattoo33 commented 2 years ago

Thank you for helping me out I really appreciate it. This is the first thing i tried as it made sense and it didnt work then but its working now hehe