Apollo-client: Invalid variable referenced in @include directive

Created on 25 Jan 2019  路  2Comments  路  Source: apollographql/apollo-client

I'm duplicating this issue because I've encountered it and it appears the issue was lost and needed to be recreated (ref: https://stackoverflow.com/questions/46004129/invalid-variable-referenced-in-include-directive)

Quoted below vvv

I'll duplicate my issue here hoping that someone may give me a hint to where I'm going wrong.

Here's the schema on GraphCool:

type Reservation implements Node {
  createdAt: DateTime!
    id: ID!@isUnique
  updatedAt: DateTime!
    user: User @relation(name: "UserReservations")
}

type User implements Node {
  createdAt: DateTime!
    id: ID!@isUnique
  updatedAt: DateTime!
    reservations: [Reservation!] !@relation(name: "UserReservations")
}

Here's a fragment in my Angular code:

fragment reservation on Reservation {
  id,
  user @include(
    if :$is_admin) {
    id
  }
}

Here's my GraphQL query:

mutation($id: ID!, $is_admin: Boolean = true) {
  createReservation(userId: $id) {
    ...reservation
  }
}
$ {
  RESERVATION_FRAGMENT.reservation
}

Calling the above mutation using apollo.watchQuery executes successfully on GraphCool, but the client produces the following error:

Error: Invalid variable referenced in @include directive.
at directives.js: 42
at Array.some( < anonymous > )
at Object.shouldInclude(directives.js: 20)
at graphql.js: 27
at Array.forEach( < anonymous > )
at executeSelectionSet(graphql.js: 26)
at graphql.js: 55
at Array.forEach( < anonymous > )
at executeSelectionSet(graphql.js: 26)
at graphql.js: 94

Most helpful comment

I created a currently failing test that reproduces the issue. The issue reproduces when calling graphql-anywhere's filter, propType, 'check', or even graphql with a fragment that uses a variable.

Here's a fairly simple reproducible sample:

import gql from 'graphql-tag';
import propType from 'graphql-anywhere';

const fragment = gql`
  fragment visitor on Visitor {
    pageViews
    user @include(if: $isAuthenticated) {
      username
      name
      email
    }
  }
`;

const propTypes = {
    visitor: propType(fragment),
};

All 2 comments

Could you please provide a minimal reproducible sample?

I created a currently failing test that reproduces the issue. The issue reproduces when calling graphql-anywhere's filter, propType, 'check', or even graphql with a fragment that uses a variable.

Here's a fairly simple reproducible sample:

import gql from 'graphql-tag';
import propType from 'graphql-anywhere';

const fragment = gql`
  fragment visitor on Visitor {
    pageViews
    user @include(if: $isAuthenticated) {
      username
      name
      email
    }
  }
`;

const propTypes = {
    visitor: propType(fragment),
};

Was this page helpful?
0 / 5 - 0 ratings