繁体   English   中英

ApolloGraphQL:graphql() 没有激活解析器?

[英]ApolloGraphQL: graphql() Not Activating Resolver?

我需要进行服务器到服务器 graphQL 调用。 感谢在此 SO 帖子上收到的建议,并在此处记录,我正在接近它:

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;
}

它没有抛出任何错误,但它返回 null 数据:

正在返回空数据

此外,解析器中的debugger断点没有被命中。

架构

cronJobToFindUsersWhoHaveGoneOffline(timeStarted: String): epUserData

询问

// 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
        },
    }
`;

解析器

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);
        });
},

我错过了什么?

它应该工作。 这是一个完整的工作示例:

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;
  });
});

集成测试结果:

 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

源代码: https://github.com/mrdulin/apollo-graphql-tutorial/tree/master/src/stackoverflow/62122142

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM