Skip to content

Error when performing queries with variable inside array literal #95

@mazei513

Description

@mazei513

When running a query with variables inside array literal, an error Variable "$varName" is not defined by operation "Query_fieldName".. For example,

# With this schema
extend type Query {
  searchUsers (ids: [ID!]!): [User]
}

# The following query fails
query search($userId: ID!) {
  searchUsers(ids: [$userId]) {
    id
  }
}

Operations like these work on Apollo Server. When I added a similar query into the base mercurius repo's tests, it also passes. Only in mercurius-gateway it fails.

The following test fails.

test('It support variable inside array literal', async t => {
  const users = [{
    id: 'u1',
    name: {
      firstName: 'John',
      lastName: 'Doe'
    }
  }, {
    id: 'u2',
    name: {
      firstName: 'Jane',
      lastName: 'Doe'
    }
  }]

  const [userService, userServicePort] = await createService(
    t,
    `
    directive @customDirective on FIELD_DEFINITION

    extend type Query {
      searchUsers (ids: [ID!]!): [User]
    }

    type User @key(fields: "id") {
      id: ID!
      name: UserName!
    }

    type UserName {
      firstName: String!
      lastName: String!
    }
  `,
    {
      Query: {
        searchUsers: (root, args) => {
          return users.filter((u) => args.ids.includes(u.id))
        }
      }
    }
  )

  const gateway = Fastify()
  t.teardown(async () => {
    await gateway.close()
    await userService.close()
  })

  await gateway.register(plugin, {
    gateway: {
      services: [
        {
          name: 'user',
          url: `http://localhost:${userServicePort}/graphql`
        }
      ]
    }
  })

  const query = `
  query MainQuery(
    $userId: ID!
  ){
    searchUsers (ids: [$userId]) {
      id
      name {
        firstName
        lastName
      }
    }
  }`

  const res = await gateway.inject({
    method: 'POST',
    headers: {
      'content-type': 'application/json'
    },
    url: '/graphql',
    body: JSON.stringify({
      query,
      variables: {
        userId: 'u1'
      }
    })
  })

  t.same(JSON.parse(res.body), {
    data: {
      searchUsers: [users[0]]
    }
  })
})

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions