0
votes

I need to do a server-to-server graphQL call. Thanks to the advice received on this SO post, and also documented here, I'm approaching it like this:

async function () {
    const {data, errors} = await graphql(
        schema,
        CRON_JOB_TO_FIND_USERS_WHO_HAVE_GONE_OFFLINE_MUTATION,
        {},
        {caller: 'synced-cron'},
        {timeStarted: new Date().toISOString().slice(0, 19).replace('T', ' ')}
    )
    console.log('data', data)
    console.log('errors', errors)

    return true;
}

It's not throwing any errors, but it's returning null data:

Null data is being returned

Also, a debugger breakpoint in the resolver isn't being hit.

SCHEMA

cronJobToFindUsersWhoHaveGoneOffline(timeStarted: String): epUserData

QUERY

// note -- no gql``. This string is passed directly to graphql() function
// where it gets gql applied to it.
const CRON_JOB_TO_FIND_USERS_WHO_HAVE_GONE_OFFLINE_MUTATION = `
    mutation ($timeStarted: String){
        cronJobToFindUsersWhoHaveGoneOffline(timeStarted: $timeStarted){
                id,
                user_presence,
                user_presence_time_of_last_update
        },
    }
`;

RESOLVER

cronJobToFindUsersWhoHaveGoneOffline(parent, args, context){
    debugger; <== NEVER GETS ACTIVATED

    return Promise.resolve()
        .then(() => {
            debugger;
            //CODE TO FIND USERS AND MARK THEM AS BEING OFFLINE GOES HERE
            return usersWhoWentOffline;
        })
        .then((usersWhoWentOffline) => {
            debugger;
            return usersWhoWentOffline;
        })
        .catch((err) => {
            debugger;
            console.log(err);
        });
},

What am I missing?

1
Typically this means the resolver is not being included in your schema in the first place. That can be an issue with how you're importing/exporting the resolver, or any number of other issues. Please show all the relevant code.Daniel Rearden
That fixed it! I had a typo in my statement that imported the resolver. If you would like to post your comment as an answer, I will mark it as the accepted answer. Thanks again!VikR

1 Answers

1
votes

It should work. Here is a completed working example:

server.ts:

import { ApolloServer } from 'apollo-server';
import { schema } from './schema';

const server = new ApolloServer({ schema });

export { server };

schema.ts:

import { gql, makeExecutableSchema } from 'apollo-server';

const typeDefs = gql`
  type EpUserData {
    id: ID!
    user_presence: String
    user_presence_time_of_last_update: String
  }
  type Query {
    dummy: String
  }
  type Mutation {
    cronJobToFindUsersWhoHaveGoneOffline(timeStarted: String): EpUserData
  }
`;

const resolvers = {
  Mutation: {
    async cronJobToFindUsersWhoHaveGoneOffline(parent, args, context) {
      const usersWhoWentOffline = { id: 1, user_presence: 'test', user_presence_time_of_last_update: '2020' };
      return Promise.resolve()
        .then(() => {
          return usersWhoWentOffline;
        })
        .catch((err) => {
          console.log(err);
        });
    },
  },
};

const schema = makeExecutableSchema({ typeDefs, resolvers });

export { schema };

server.test.ts:

import { graphql } from 'graphql';
import { schema } from './schema';
import { server } from './server';

const CRON_JOB_TO_FIND_USERS_WHO_HAVE_GONE_OFFLINE_MUTATION = `
    mutation ($timeStarted: String){
        cronJobToFindUsersWhoHaveGoneOffline(timeStarted: $timeStarted){
                id,
                user_presence,
                user_presence_time_of_last_update
        },
    }
`;

describe('62122142', () => {
  beforeAll(async () => {
    const { url } = await server.listen();
    console.log(`server is listening on ${url}`);
  });
  afterAll(async () => {
    await server.stop();
  });
  it('should pass', async () => {
    const { data, errors } = await graphql(
      schema,
      CRON_JOB_TO_FIND_USERS_WHO_HAVE_GONE_OFFLINE_MUTATION,
      {},
      { caller: 'synced-cron' },
      {
        timeStarted: new Date()
          .toISOString()
          .slice(0, 19)
          .replace('T', ' '),
      },
    );
    console.log('data', data);
    console.log('errors', errors);

    return true;
  });
});

integration test result:

 PASS   apollo-graphql-tutorial  src/stackoverflow/62122142/server.test.ts (7.143s)
  62122142
    ✓ should pass (12ms)

  console.log src/stackoverflow/62122142/server.test.ts:18
    server is listening on http://localhost:4000/

  console.log src/stackoverflow/62122142/server.test.ts:36
    data [Object: null prototype] {
      cronJobToFindUsersWhoHaveGoneOffline:
       [Object: null prototype] {
         id: '1',
         user_presence: 'test',
         user_presence_time_of_last_update: '2020' } }

  console.log src/stackoverflow/62122142/server.test.ts:37
    errors undefined

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        7.226s

source code: https://github.com/mrdulin/apollo-graphql-tutorial/tree/master/src/stackoverflow/62122142