How do I resolve connections on GraphQL Types using express-graphql?

Viewed 597

I've begun writing a schema for supporting GraphQL connections on my server, however, I'm not understanding how to populate the connection with data for the response.

I'm using a single root query design. I'm not sure if this can support more complex structures such as connections and pagination, or if I need to use GraphQLObjectTypes.

Here's what my graphql server looks like:

import express from 'express';
import cors from 'cors';
import graphqlHTTP from 'express-graphql';

// Functions that call legacy REST endpoints
import { accounts } from './api/accounts';
import { user } from './api/users';
import schema from './schema';

// GraphQL API
const root = {
  user,
  accounts,
};

const app = express();
app.use(cors());

app.use('/graphql', graphqlHTTP({
  schema,
  rootValue: root,
  graphiql: true
}));

And this is my schema:

import {
  buildSchema,
} from 'graphql';

export default buildSchema(`
  type PageInfo {
    startCursor: String
    endCursor: String
    hasNextPage: Boolean
  }

  type User {
    firstName: String
    lastName: String
    personId: Int
    accountsConnection(
      first: Int,
      after: String,
      last: Int,
      before: String
    ): UserAccountsConnection
  }

  type UserAccountsConnection {
    pageInfo: PageInfo!
    edges: [UserAccountsEdge]
    totalCount: Int
  }

  type UserAccountsEdge {
    cursor: String!
    node: Account
  }

  type Account {
    accountName: String
    accountId: ID
  }

  type Query {
    user(personId: Int): User
    users: [User]!
    accounts: [Account]!
    accountsConnection(first: Int): UserAccountsConnection
  }
`);

I'm receiving null for the accountsConnection.

enter image description here

0 Answers
Related