首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ApolloGraphQL: graphql()没有激活Resolver?

ApolloGraphQL: graphql()没有激活Resolver?
EN

Stack Overflow用户
提问于 2020-05-31 20:34:03
回答 1查看 203关注 0票数 0

我需要做一个服务器到服务器的graphQL呼叫。感谢在这是如此的帖子上收到的建议,以及文档化的这里,我这样对待它:

代码语言:javascript
复制
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;
}

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

另外,解析器中的debugger断点没有被击中。

模式

代码语言:javascript
复制
cronJobToFindUsersWhoHaveGoneOffline(timeStarted: String): epUserData

查询

代码语言:javascript
复制
// 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

代码语言:javascript
复制
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);
        });
},

我遗漏了什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-07-20 03:44:03

应该管用的。下面是一个完整的工作示例:

server.ts

代码语言:javascript
复制
import { ApolloServer } from 'apollo-server';
import { schema } from './schema';

const server = new ApolloServer({ schema });

export { server };

schema.ts

代码语言:javascript
复制
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

代码语言:javascript
复制
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;
  });
});

集成测试结果:

代码语言:javascript
复制
 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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62122142

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档