简体   繁体   English

如何从解析器中的 graphql 获取数据

[英]how can I fetch data from graphql in my resolver

Within my resolver I seem to be unable to fetch connected data在我的解析器中,我似乎无法获取连接的数据

this works in the graphql playground (prisma) but I am unsure of the syntax about how to form a resolver in apollo server这适用于 graphql 游乐场(prisma),但我不确定有关如何在 apollo 服务器中形成解析器的语法

// my typedef for activity is

type Activity {
    id: ID! @id
    ActivityType: ActivityType!
    title: String!
    date: DateTime
    user: User!
    distance: Float!
    distance_unit: Unit!
    duration: Float!
    elevation: Float
    elevation_unit: Unit
    createdAt: DateTime! @createdAt
    updatedAt: DateTime! @updatedAt

// and my resolver currently looks like this

async activity(parent, args, ctx, info) {
        const foundActivity = await ctx.db.query.activity({
            where: {
                id: args.id
            }
        });
        // todo fetch user data from query
        console.log(foundActivity);
    }

// where db has been placed onto the ctx (context)
// the CL gives all of the data for Activity apart from the user

// in the playground I would do something like this

query activity {
  activity(where: {
    id: "cjxxow7c8si4o0b5314f9ibek"
  }){
    title
    user {
      id
      name
    }
  }
}

// but I do not know how to specify what is returned in my resolver.

console.log(foundActivity) gives:

{ id: 'cjxxpuh1bsq750b53psd2c77d',
  ActivityType: 'CYCLING',
  title: 'Test Activity',
  date: '2019-07-10T20:21:27.681Z',
  distance: 13.4,
  distance_unit: 'KM',
  duration: 90030,
  elevation: 930,
  elevation_unit: 'METERS',
  createdAt: '2019-07-10T20:48:50.879Z',
  updatedAt: '2019-07-10T20:48:50.879Z' }

Prisma is the DB ORM and then I have an Apollo-Server 2 server running on top of that. Prisma 是 DB ORM,然后我有一个 Apollo-Server 2 服务器运行在它之上。 Unfortunately, stack overflow also thinks that there is too much code on this post so I will have to waffle on about inconsequential gibberish due to the fact that their system can't handle it.不幸的是,堆栈溢出也认为这篇文章中有太多代码,所以我将不得不因为他们的系统无法处理这些无关紧要的胡言乱语而胡思乱想。

You will have to implement a resolver for Activity.user .您必须为Activity.user实现一个解析器。 Unfortunately your entity does not seem to contain a reference to the user.不幸的是,您的实体似乎不包含对用户的引用。 First, add the user connection to your Prisma data model.首先,将用户连接添加到您的 Prisma 数据模型。 Then implement a resolver for Activity.user .然后为Activity.user实现一个解析器。 I am not very familiar with Prisma 1 but this naive implementation should already do what you want:我对 Prisma 1 不是很熟悉,但是这个简单的实现应该已经可以做你想做的了:

let resolvers = {
  Query: {
    // ...
  },
  Activity: {
    user(parent, args, ctx) {
      return ctx.db.query.activity({ id: parent.id }).user();
    }
  }
}

Find out more about resolving relations in Prisma here此处了解有关解决 Prisma 关系的更多信息

So the answer was incredibly simple: I just add a second argument to the query (after the "where" with a gql tag of the data shape to be returned so my code now looks like:所以答案非常简单:我只是向查询添加第二个参数(在“where”之后,带有要返回的数据形状的 gql 标记,因此我的代码现在看起来像:

const foundActivity = await ctx.db.query.activity(
        {
            where: {
                id: args.id
            }
        },
        `{id title user { id name }}`
    );

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

相关问题 如何从 graphql 中的解析器数据库中获取数据 - How to fetch the data from database for resolver in graphql 如何使用我的 Typescript 解析器对这个 Graphql 数组进行排序? - How can I sort this Graphql array with my Typescript resolver? 如何在 Express GraphQL 中的 2 个解析器函数之间共享数据? - How can I share data between 2 resolver functions in Express GraphQL? GraphQL如何仅在需要时使解析器获取数据? - GraphQL how to make resolver fetch data only if needed? 我可以将 args 的类型从 graphql 的解析器更改为 id 吗? - Can I change the type of args from graphql's resolver to id? 我如何在需要时将数据提取到 graphql 服务器 - How can I fetch data to graphql server when I want 如何在 graphql-js 中为 object 类型定义解析器? - How can I define resolver for object type in graphql-js? 我的 Apollo GraphQL 旋转变压器如何“向前看”孩子,而不会失去传统旋转变压器结构的好处? - How can my Apollo GraphQL resolver 'lookahead' to children without losing the benefits of a traditional resolver structure? 如何在NestJS中的GraphQL解析器内部设置会话密钥? - How can I set a session key from inside a GraphQL resolver in NestJS? 我如何在没有express的情况下获取节点graphql解析器的标头? - How can I get header for node graphql resolver without express?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM