Tufan Butuner
Tufan Butuner

Reputation: 15

GraphQLError: Syntax Error: Unexpected "("

I am new to GraphQL and am trying to query the TfL API for a project with React. I am trying to pass $arrival as a variable into the query, dynamically calling arrivals by station ID. It was working yesterday but this morning it is throwing a syntax error, any ideas where it may be coming from?

Type Defs

const typeDefs = `
  type Station {
    id: String!
    stationName: String
    lineId: String
    lineName: String
    platformName: String
    direction: String
    timestamp: String
    timeToStation: Int
    currentLocation: String
    towards: String
    expectedArrival: String
    modeName: String
  }
  
  type Query($arrival: String!) {
    getArrivals(id: $arrival): [Station]
    station: [Station]
  }
`;

Query


const GET_ALL_ARRIVALS = gql`
  query Arrivals($id: String!) {
    getArrivals(id: $id) {
      id
      stationName
      platformName
      lineName
      towards
      timeToStation
    }
  }
`;

Upvotes: 1

Views: 3726

Answers (2)

David Maze
David Maze

Reputation: 158696

When you declare the top-level query type, you're trying to embed variable definitions as part of the type definition. They don't belong here; they are purely part of the query. $variable references never appear anywhere in the schema.

type Query {        # does not take any parameters
  getArrivals(id: ID!): [Station]
  #               ^^^ specify the _type_ of the field argument here
}
query Arrivals($someId: ID!) { # variable definition is part of the query
  getArrivals(id: $someId) {   # bind variable to field argument
    id, ...
  }
}

Upvotes: 1

flzzz
flzzz

Reputation: 543

Try changing your schema to

const typeDefs = `
  type Station {
    id: String!
    stationName: String
    lineId: String
    lineName: String
    platformName: String
    direction: String
    timestamp: String
    timeToStation: Int
    currentLocation: String
    towards: String
    expectedArrival: String
    modeName: String
  }
  
  type Query {
    getArrivals(id: String!): [Station]
    station: [Station]
  }
`;

That doesn't explain the error, but it seems wrong to me in comparison to the documentation

Upvotes: 1

Related Questions