question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to use in GraphQL.js GraphQLSchema object way?

See original GitHub issue

This 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:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:17 (17 by maintainers)

github_iconTop GitHub Comments

2reactions
Hongbo-Miaocommented, Sep 17, 2020

Post full solution here to help future people save some time. Maybe we can add this to readme too : )

import { execute, subscribe, GraphQLObjectType, GraphQLSchema, GraphQLString } from 'graphql';
import { createServer } from 'graphql-transport-ws';
import { PubSub } from 'graphql-subscriptions';

const pubsub = new PubSub();

const subscription = new GraphQLObjectType({
  name: 'Subscription',
  fields: {
    greeting: {
      type: GraphQLString,
      resolve: (source) => {
        if (source instanceof Error) {
          throw source;
        }
        return source.greeting;
      },
      subscribe: () => {
        return pubsub.asyncIterator('greeting');
      },
    },
  },
});

const query = new GraphQLObjectType({
  name: 'Query',
  fields: {
    dummy: {
      type: GraphQLString,
      resolve: () => {
        return 'dummy';
      },
    },
  },
});

const schema = new GraphQLSchema({
  query,
  subscription,
});

setInterval(() => {
  pubsub.publish('greeting', {
    greeting: 'Bonjour',
  });
}, 1000);

createServer(
  {
    schema,
    execute,
    subscribe,
  },
  {
    server,
    path: '/graphql',
  }
);
1reaction
enisdenjocommented, Oct 2, 2020

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. 😄

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found