How to use in GraphQL.js GraphQLSchema object way?
See original GitHub issueThis is my client:
const client = createClient({
url: 'wss://localhost:5000/graphql',
});
client.subscribe(
{
query: 'subscription { greeting }',
},
{
next: (data) => {
console.log('data', data);
},
error: (error) => {
console.error('error', error);
},
complete: () => {
console.log('no more greetings');
},
}
);
I succeed by GraphQL Schema Definition Language way just like the demo in readme (I only changed greetings to greeting). Got 101 Switching Protocols. And the client will print out the greeting five times in the browser console.
Server (Succeed version)
const schema = buildSchema(`
type Subscription {
greeting: String
}
`);
// The roots provide resolvers for each GraphQL operation
const roots = {
subscription: {
greeting: async function* sayHiIn5Languages() {
for (const hi of ['Hi', 'Bonjour', 'Hola', 'Ciao', 'Zdravo']) {
yield { greeting: hi };
}
},
},
};
createServer(
{
schema,
execute,
subscribe,
roots,
},
{
server,
path: '/graphql',
}
);
However, I failed on GraphQL.js GraphQLSchema object way. Got 101 Switching Protocols. But the client never prints out the greeting in the browser console.
Server (Failed version)
import { execute, subscribe, GraphQLObjectType, GraphQLSchema, GraphQLString } from 'graphql';
import { createServer } from 'graphql-transport-ws';
const GreetingGraphQLType = new GraphQLObjectType({
name: 'Greeting',
fields: {
greeting: { type: GraphQLString },
},
});
const subscription = new GraphQLObjectType({
name: 'Subscription',
fields: {
greeting: {
type: GreetingGraphQLType,
resolve: async function* sayHiIn5Languages() {
for (const hi of ['Hi', 'Bonjour', 'Hola', 'Ciao', 'Zdravo']) {
yield { greeting: hi };
}
},
},
},
});
const schema = new GraphQLSchema({
subscription,
});
createServer(
{
schema,
execute,
subscribe,
},
{
server,
path: '/graphql',
}
);
Any idea? Thanks
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:17 (17 by maintainers)
Top Results From Across the Web
Three ways to represent your GraphQL schema
These formats are: The GraphQL Schema Definition Language, or SDL; The GraphQL introspection query result; The GraphQL.js GraphQLSchema object.
Read more >Object Types - GraphQL
In GraphQL schema language, the way you define a new object type is the same ... have fields that return a particular type,...
Read more >Getting Started With GraphQL.js
To create a new project and install GraphQL.js in your current directory: npm init ... Construct a schema, using GraphQL schema language.
Read more >Constructing Types - GraphQL
You can do this using the GraphQLSchema constructor. When you are using the GraphQLSchema constructor to create a schema, instead of defining Query...
Read more >Schemas and Types - GraphQL
We'll use the "GraphQL schema language" - it's similar to the query ... same as any other GraphQL object type, and their fields...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Post full solution here to help future people save some time. Maybe we can add this to readme too : )
Aha, now I follow. I use the schema parsing for the sake of simplicity. I try to bring more focus on the lib itself, not the semantics of GraphQL. 😄