GraphQLError: Syntax Error: Unexpected "(" - graphql

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
}
}
`;

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

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, ...
}
}

Related

GraphQL mutation takes an array as an input parameter and returns a json String

I'm trying to implement mutation query with an array as a parameter and String as a return type.
Here is my schema file:
input OrganizationInput {
orgId: String!
orgName: String!
}
type Mutations {
importOrganizations(input: [OrganizationInput]): String
}
Here is my mutation:
mutation importOrganizations($orgs: [OrganizationInput]) {
importOrganizations(
input: {
orgId: id,
orgName: name
}
)
}
This code doesn't work, but I don't know how to do it properly.
Maybe someone more experienced in GraphQL could help me?
Do you have any errors that can help?
Anyways your mutation need to return fields, e.g.:
mutation importOrganizations($orgs: [OrganizationInput]) {
importOrganizations(
input: {
orgId: id,
orgName: name
})
{
id
name
}
}

Wrong type return in Resolver does not trigger error

I am building a sever with Graphql. I define a single query which should return a User type. However, in my resolver I am actually returning a string type: "Hello!".
The query executes in the playground without any problem.
My question: the return statement of the resolver expects a User type, instead returns a string. Why is it not failing?
TypeDef
type User {
id: String
email: String
name: String
}
type Query {
printUser: User
}
Resolver
const resolvers = {
Query: {
printUser: async (parent, args, context, info) => {
return 'Hello!';
}
}

GraphQL query with multiple nested resolvers and mapping fields to arguments

From GraphQL Client's perspective, how do I perform a query with multiple nested resolvers where the fields from the parent are passed as arguments to the child resolver?
Here is a minimal example:
GraphQL Schema:
type Author {
id: ID!
name: String!
}
type Book {
id: ID!
title: String!
releaseDate: String!
}
type Query {
// Returns a list of Authors ordered by name, 'first' indicates how many entries to return
getAllAuthors(first: Int!): [Author]!
// Returns a list of Books ordered by releaseDate, 'first' indicates how many entries to return
getBooksByAuthorId(first: Int! authorId: ID!): [Book]!
}
Is it possible to write a query to get all authors and their last released book? Something around the lines:
query GetAuthorsWithLastBook($first: Int!) {
getAllAuthors(first: $first) {
authorId: id
name
lastBook: getBooksByAuthor(1, authorId) {
title
}
}
}
In the example above, I attempted to alias getAllAuthors.id as authorId and pass the alias down as argument to getBooksByAuthor(...) but that didn't work.
The key aspect of the problem is that I don't know the authorIds beforehand. I could fetch the authors first and build a query to fetch their last book but that will result in multiple queries and that is something I would like to avoid.
Update
A Java Kickstarter example is available here: https://www.graphql-java-kickstart.com/tools/schema-definition/
yes, on the graphql definition, you need to add lastBook in the Author
type Author {
id: ID!
name: String!
lastBook: [Book]
}
Next up u need to write the resolver for the lastBook
const resolvers = {
Query: {
Author {
lastBook: (parent, args) {
const userId = parent.id;
return getBooksByAuthor(userId, 1);
},
}
}
};

Dynamically choose query variables in React Apollo

I'd like to be able to dynamically choose which query variables I use in GraphQL.
For example, it seems a little redundant to need three separate queries:
const getAllStops = gql`
query trafficStops {
trafficStops {
id
date
}
}
`
const getStopsAfter = gql`
query trafficStops($after: String!) {
trafficStops(after: $after) {
id
date
}
}
`
const getStopsBefore = gql`
query trafficStops($before: String!) {
trafficStops(before: $before) {
id
date
}
}
`
Is there a way in which I could pass not just the variables before or after but whether I'd like to use one, the other, neither, or both into a single query instead of having multiple queries?
Yes, you just have to make your arguments optional. The exclamation mark at String! requires the argument to be a string and not null. Hence, by removing it you could write your single query as
const getAllStops = gql`
query trafficStops($after: String, $before: String) {
trafficStops(after: $after, before: $before) {
id
date
}
}
`

How to change the graphql typedefs name that is coming from json results of external api?

I have encountered an issue with the naming in my typedefs. The error prompting the following.
Syntax Error: Expected Name, found Int "24"
I am using Coinmarketcap Api and accessing it with my apollo graphql server. The api has naming such as 24h_volume_usd, percent_change_1h etc, but as long as integer within the name, it will have this name issue.
I am not really sure how can I fix this issue. Can anyone please help me out? Thank you very much.
Schema.js:
const typeDefs = `
type cryptos {
id: String
name: String
symbol: String
rank: String
price_usd: String
price_btc: String
24h_volume_usd: String
market_cap_usd: String
percent_change_1h: String
available_supply: String
total_supply: String
last_updated: String
}
type Query {
cryptos: [cryptos]
}
`
resolvers.js:
const resolvers = {
Query: {
cryptos: () => {
return axios.get('https://api.coinmarketcap.com/v1/ticker/').
then(result => result.data );
}
}
Process api result using map functions
`var newHashmap = {};
Object.keys(hashmap).forEach(function(key){
var value = hashmap[key];
key = key + "xxx";
console.log("changing:");
console.log(key); newHashmap[key] = value
});`

Resources