Reputation: 2191
I’m actually unsure why, but I can’t seem to fix this seemingly simple problem. I have a graphql schema as follows:
import { gql } from 'apollo-server-core';
export const querySchema = gql`
type AffiliateBalance {
pendingBalance: Float!
qualifiedBalance: Float!
}
type Query {
me: User!
users(input: PaginationInput): [User!]!
user(id: Int!): User!
referrals(input: GetReferralsInput): GetReferralsResponse!
affiliateTransactions(
limit: Int
skip: Int
type: TransactionType
): GetAffiliateTransactionsResponse!
affiliatePerformanceMetrics: AffiliatePerformanceMetricsResponse!
affiliateSessions(limit: Int, skip: Int): GetAffiliateSessionsResponse!
affiliateSessionMetrics: AffiliateSessionMetricsResponse!
affiliateBalancePending: Float!
affiliateBalanceQualified: Float!
affiliateBalance: Float! | AffiliateBalance!
}
`;
I have recently added an affiliateBalance
query, which can either return a number to 2DP (e.g. 47.28), or this object:
`{ pendingBalance: some2DPNumber, qualifiedBalance: some2DPNumber }`
But I am getting this error:
return new _GraphQLError.GraphQLError(`Syntax Error: ${description}`, {
^
GraphQLError: Syntax Error: Expected Name, found "|"
What am I doing wrong here? And if it is supposedly a syntax error with the "|" symbol, how can it actually be fixed? The result of the affiliateBalance
query needs to be a union, hence the symbol.
I’m using Apollo Server with node. Thank you
UPDATE:
The error I get with the code in my question as requested:
/Users/nick/Documents/Coding/Projects/Practice/PERNTG/server/node_modules/graphql/error/syntaxError.js:15 return new _GraphQLError.GraphQLError(
Syntax Error: ${description}, { ^ GraphQLError: Syntax Error: Expected Name, found "|". at syntaxError (/Users/nick/Documents/Coding/Projects/Practice/PERNTG/server/node_modules/graphql/error/syntaxError.js:15:10) at Parser.expectToken (/Users/nick/Documents/Coding/Projects/Practice/PERNTG/server/node_modules/graphql/language/parser.js:1413:40) at Parser.parseName (/Users/nick/Documents/Coding/Projects/Practice/PERNTG/server/node_modules/graphql/language/parser.js:107:24) at Parser.parseFieldDefinition (/Users/nick/Documents/Coding/Projects/Practice/PERNTG/server/node_modules/graphql/language/parser.js:845:23) at Parser.optionalMany (/Users/nick/Documents/Coding/Projects/Practice/PERNTG/server/node_modules/graphql/language/parser.js:1510:28) at Parser.parseFieldsDefinition (/Users/nick/Documents/Coding/Projects/Practice/PERNTG/server/node_modules/graphql/language/parser.js:831:17) at Parser.parseObjectTypeDefinition (/Users/nick/Documents/Coding/Projects/Practice/PERNTG/server/node_modules/graphql/language/parser.js:803:25) at Parser.parseDefinition (/Users/nick/Documents/Coding/Projects/Practice/PERNTG/server/node_modules/graphql/language/parser.js:171:23) at Parser.many (/Users/nick/Documents/Coding/Projects/Practice/PERNTG/server/node_modules/graphql/language/parser.js:1529:26) at Parser.parseDocument (/Users/nick/Documents/Coding/Projects/Practice/PERNTG/server/node_modules/graphql/language/parser.js:121:25) { path: undefined, locations: [ { line: 22, column: 30 } ], extensions: [Object: null prototype] {} }
Then if I change it to this:
import { gql } from 'apollo-server-core';
export const querySchema = gql`
type AffiliateBalance {
pendingBalance: Float!
qualifiedBalance: Float!
}
union RawAffiliateBalance = Float | AffiliateBalance
type Query {
me: User!
users(input: PaginationInput): [User!]!
user(id: Int!): User!
referrals(input: GetReferralsInput): GetReferralsResponse!
affiliateTransactions(
limit: Int
skip: Int
type: TransactionType
): GetAffiliateTransactionsResponse!
affiliatePerformanceMetrics: AffiliatePerformanceMetricsResponse!
affiliateSessions(limit: Int, skip: Int): GetAffiliateSessionsResponse!
affiliateSessionMetrics: AffiliateSessionMetricsResponse!
affiliateBalancePending: Float!
affiliateBalanceQualified: Float!
affiliateBalance: RawAffiliateBalance!
}
`;
I get this error:
/Users/nick/Documents/Coding/Projects/Practice/PERNTG/server/node_modules/graphql/type/validate.js:59
throw new Error(errors.map((error) => error.message).join('\n\n'));
^
Error: Union type RawAffiliateBalance can only include Object types, it cannot include Float.
at assertValidSchema (/Users/nick/Documents/Coding/Projects/Practice/PERNTG/server/node_modules/graphql/type/validate.js:59:11)
at assertValidExecutionArguments (/Users/nick/Documents/Coding/Projects/Practice/PERNTG/server/node_modules/graphql/execution/execute.js:194:35)
at execute (/Users/nick/Documents/Coding/Projects/Practice/PERNTG/server/node_modules/graphql/execution/execute.js:113:3)
at generateSchemaHash (/Users/nick/Documents/Coding/Projects/Practice/PERNTG/server/node_modules/apollo-server-core/src/utils/schemaHash.ts:25:25)
at ApolloServer.generateSchemaDerivedData (/Users/nick/Documents/Coding/Projects/Practice/PERNTG/server/node_modules/apollo-server-core/src/ApolloServer.ts:716:42)
at Object.schemaDerivedDataProvider (/Users/nick/Documents/Coding/Projects/Practice/PERNTG/server/node_modules/apollo-server-core/src/ApolloServer.ts:333:18)
at new SchemaManager (/Users/nick/Documents/Coding/Projects/Practice/PERNTG/server/node_modules/apollo-server-core/src/utils/schemaManager.ts:76:36)
at new ApolloServerBase (/Users/nick/Documents/Coding/Projects/Practice/PERNTG/server/node_modules/apollo-server-core/src/ApolloServer.ts:328:24)
at new ApolloServer (/Users/nick/Documents/Coding/Projects/Practice/PERNTG/server/node_modules/apollo-server-express/src/ApolloServer.ts:55:1)
at /Users/nick/Documents/Coding/Projects/Practice/PERNTG/server/src/index.ts:20:48
Upvotes: 0
Views: 639
Reputation: 4687
I believe you should be able to define a union type (scroll down to find the section, they did not use ids for easy hashing in the documentation)
about like this:
type AffiliateBalance {
pendingBalance: Float!
qualifiedBalance: Float!
}
union RawAffiliateBalance = Float | AffiliateBalance
type Query {
//...
affiliateBalance: RawAffiliateBalance!
Since Apollo gql is strictly interpreting the graphql specification (see https://github.com/graphql/graphql-spec/issues/215 ) and this has been an unresolved but actively debated issue for at least 7 years now, I suggest you might try boxing your scalar to work around the issue in graphql.
type AffiliateBalance {
pendingBalance: Float!
qualifiedBalance: Float!
}
type RawBalance {
value: Float!
}
union RawAffiliateBalance = RawBalance | AffiliateBalance
type Query {
//...
affiliateBalance: RawAffiliateBalance!
This requires revising the response payload you were hoping to serve to clients, and that may be more work than intended. Another possibility (aimed to avoid changing clients) would be to wrap it like this and expose an adapter endpoint that is not graphql and just transforms the final value for you.
Upvotes: 2