Apollo-client: cannot pass value to parameter typed as ID

Created on 22 Dec 2016  路  8Comments  路  Source: apollographql/apollo-client

Is it anyhow possible to pass a value to an ID typed parameter like below ?

I am trying to get repository information from GitHub. Therefore I created a parameterized query that expects $id of type ID!

Whenever I try to pass an ID Value like "MDEwOlJlcG9zaXRvcnk1NDc4MjE2MQ==" I get this:

Error: GraphQL error: Variable id of type ID! was provided invalid value

When I switch the parameter type from ID to String I get:

Error: GraphQL error: Type mismatch on variable $id and argument id (String / ID!)

Here's the query definition:

private repositoryByIdQuery: any = gql`
      query repositoryById($id: ID!){
        node(id: $id){
           ...repositoryFragment
        }
      }

      fragment repositoryFragment on Repository {
        id
        description
        isPrivate
        name
      }
    `

Here's the executing code:

public getById(id: string): Promise<any> {

  return new Promise<any>(
    (resolve, reject) => {
      this.githubClient.query({
            query: this.repositoryByIdQuery,
            variables:{
              id: id
            }
        }).then(
        ({loading, data}) => {
        if(!loading){
          console.log(data); 
          resolve(data);
        }
      },(reason) => {
        console.log("hello from rejected"+  reason);               
        reject(reason);
      });
    }
  )
}

Most helpful comment

@shtse8

If your schema looks something like this:

    type Repo {
        id: ID!
        .......
        .......
    }
    type Query {
        getRepo(id: ID!) : Repo
    }

You can execute your query like this:

    query getRepo($id: ID!) {
        getRepo(id: $id) {
              id
              .......
              .......
        }
    }

Make sure you use exact same type in query that you used while defining your schema, otherwise the GraphQL parser will yell at you.

Let me know if this helps. :)

All 8 comments

here is my package.json in case versions are important to know:

  "dependencies": {
    "@types/node": "^6.0.52",
    "@types/sequelize": "^4.0.39",
    "apollo-client": "^0.5.24",
    "body-parser": "~1.15.2",
    "config": "^1.24.0",
    "cookie-parser": "~1.4.3",
    "debug": "~2.2.0",
    "express": "~4.14.0",
    "graphql": "^0.7.2",
    "graphql-server-express": "^0.4.3",
    "graphql-subscriptions": "^0.2.2",
    "graphql-tag": "^1.1.2",
    "graphql-tools": "^0.8.4",
    "jade": "~1.11.0",
    "morgan": "~1.7.0",
    "mysql": "^2.12.0",
    "node-fetch": "^1.6.3",
    "sequelize": "^3.28.0",
    "serve-favicon": "~2.3.0"
  }

sorry, now it works: I had an error in passing in a wrong value for id, now it beautifully works :-)

@miwo100 I'm in the same position with the same ID kind. What was the solution for you? I'm guessing the id field on repository objects maybe isn't actually the right one?

Just answered it myself - I was querying with query repositoryById($id: String!){ instead of query repositoryById($id: ID!){

@hzsweers I am having same concern, what should we use? ($id: String!) or ($id: ID!)?

The latter

@hzsweers the latter? But it will throws error when I query like user(id:123).

@shtse8

If your schema looks something like this:

    type Repo {
        id: ID!
        .......
        .......
    }
    type Query {
        getRepo(id: ID!) : Repo
    }

You can execute your query like this:

    query getRepo($id: ID!) {
        getRepo(id: $id) {
              id
              .......
              .......
        }
    }

Make sure you use exact same type in query that you used while defining your schema, otherwise the GraphQL parser will yell at you.

Let me know if this helps. :)

Was this page helpful?
0 / 5 - 0 ratings