Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GraphQL & Using a nested subscribe function

I am writing a graphql subscriptions server. If I write a query it is no problem to have resolvers nested one within the other, so the query would look something like this:

query {
  messages {
    privateMessage {
      id
      message
      userId
    }
  }
}

So first the messages resolver is executed, then the privateMessage resolver is executed. I would like to know if the same structure is achievable for subscriptions so it would look like this:

subscription {
  messages {
    privateMessage {
      id
      message
      userId
    }
  }
}

EDIT:

This is the current root subscription schema I have:

const RootSubscriptions = new GraphQLObjectType({
  name: 'RootSubscriptions',
  fields: {
    privateMessage: {
      type: PrivateMessage.type,
      resolve: PrivateMessage.resolve,
      subscribe: PrivateMessage.subscribe,
    },
    flaggedMessage: {
      type: FlaggedMessage.type,
      resolve: FlaggedMessage.resolve,
      subscribe: FlaggedMessage.subscribe,
    },
    teamMessage: {
      type: TeamMessage.type,
      resolve: TeamMessage.resolve,
      subscribe: TeamMessage.subscribe,
    },
  },
})

I would like it to look like this:

const RootSubscriptions = new GraphQLObjectType({
  name: 'RootSubscriptions',
  fields: {
    messages: {
      type: new GraphQLObjectType({
        name: 'MessagesSubType',
        fields: {
          privateMessage: {
            type: PrivateMessage.type,
            resolve: PrivateMessage.resolve,
            subscribe: PrivateMessage.subscribe,
          },
          flaggedMessage: {
            type: FlaggedMessage.type,
            resolve: FlaggedMessage.resolve,
            subscribe: FlaggedMessage.subscribe,
          },
          teamMessage: {
            type: TeamMessage.type,
            resolve: TeamMessage.resolve,
            subscribe: TeamMessage.subscribe,
          },
        }
      })
    }
  },
})

EDIT END

Problem is that I get the messages subscribe function to run but not the privateMessage subscribe function to run. Would love to know if it is possible and how to achieve it. Since I'm writing it with node.js, I would appreciate an example in js, but any pointer to a solution would be helpful. Thanks in advance!

like image 334
U Rogel Avatar asked Nov 06 '22 06:11

U Rogel


People also ask

What is GraphQL used for?

GraphQL is designed to make APIs fast, flexible, and developer-friendly. It can even be deployed within an integrated development environment (IDE) known as GraphiQL. As an alternative to REST, GraphQL lets developers construct requests that pull data from multiple data sources in a single API call.

What is GraphQL vs REST API?

REST: Differences. The biggest difference between GraphQL and REST is the manner in which data is sent to the client. In a REST architecture, the client makes an HTTP request and data is sent as an HTTP response, while in GraphQL, the client requests data with queries.

Is GraphQL same as SQL?

No, but this is a common misconception. GraphQL is a specification typically used for remote client-server communications. Unlike SQL, GraphQL is agnostic to the data source(s) used to retrieve data and persist changes. Accessing and manipulating data is performed with arbitrary functions called resolvers.

Is GraphQL SQL or NoSQL?

GraphQL is a flexible query language that uses a type system to efficiently return data with dynamic queries. SQL(structured query language) is an older, more adopted language standard used specifically for tabular/relational database systems. Consider GraphQL if you want your API to be built on a NoSQL database.


1 Answers

Based on my understanding of how graphQl subscriptions work, you may have little luck with nested subscriptions. I've not found supporting documentation to support this, but in my own experimentations, I've not found this to work. In this case, I would advice that you have a messages root subscription that expects parameters of the type of sucription, in this case an enum of either private, flagged or team message. You can use switch case to determine the subscription and react accordingly.

like image 142
Kisinga Avatar answered Nov 12 '22 14:11

Kisinga