After constructing a schema using type-graphql
and setting up a server with apollo-server-lambda
, then deploying to netlify functions
, I encountered an issue.
The schema generated by type-graphql
# -----------------------------------------------
# !!! THIS FILE WAS GENERATED BY TYPE-GRAPHQL !!!
# !!! DO NOT MODIFY THIS FILE BY YOURSELF !!!
# -----------------------------------------------
type Project {
name: String!
summary: String!
titleAndSummary: String!
}
type Query {
projects: [Project!]!
}
Issue
When querying the API, I expected the response to be like this
{
__schema {
types {
name
}
}
}
However, the actual response was different
{
"data": {
"__schema": {
"types": [
{
"name": "Query"
},
{
"name": "a"
},
// ...
]
}
}
}
I plan on utilizing graphql with Elm-Graphql
, which auto-generates modules using their __typename
property. This is why I need to address the above problem.
I have made several attempts to fix it but have not been able to pinpoint the source of the issue. It appears to be related to either type-graphql
, apollo-server
, or even netlify functions
(I have tested both netlify dev server and real server deployments)
Code Samples
graphql.ts
import 'reflect-metadata';
import { ApolloServer } from 'apollo-server-lambda';
import { buildSchemaSync } from 'type-graphql';
import { QueryResolver, ProjectResolver } from './resolver';
const schema = buildSchemaSync({
resolvers: [QueryResolver, ProjectResolver],
});
const server = new ApolloServer({
schema,
debug: false,
});
export const handler = server.createHandler();
types.ts
import { ObjectType, Field } from 'type-graphql';
@ObjectType()
export class Query {
@Field((type) => [Project!]!)
projects!: Project[];
}
@ObjectType()
export class Project {
@Field((type) => String!)
name!: string;
@Field((type) => String!)
summary!: string;
}
resolver.ts
import { Resolver, Query, FieldResolver, Root } from 'type-graphql';
import { Project } from './types';
import { projectList } from '../../db/fakeDB';
@Resolver()
export class QueryResolver {
@Query((returns) => [Project])
projects() {
return projectList;
}
}
@Resolver((of) => Project)
export class ProjectResolver {
@FieldResolver((returns) => String!)
titleAndSummary(@Root() project: Project) {
return project.name + ' ++ ' + project.summary;
}
}
Attempted Solution
We attempted to add __typename
directly to the Object Type as shown below
@Resolver((of) => Project)
export class ProjectResolver {
@FieldResolver((returns) => String!)
titleAndSummary(@Root() project: Project) {
return project.name + ' ++ ' + project.summary;
}
@FieldResolver((returns) => String!)
__typename() {
return 'Project';
}
}
Unfortunately, this resulted in an error during the request
◈ Error during invocation: {
errorMessage: 'Name "__typename" must not begin with "__", which is reserved by GraphQL introspection.',
errorType: 'Error',
...
How can we resolve this issue?
English is not my native language, so I apologize for any mistakes. If you notice any corrections that need to be made, please let me know. Thank you.