标签: apollo-server

如何在"GraphQL模式语言"中向字段添加描述

我有一个graphql架构,其片段如下所示:

type User {
    username: String!
    password: String!
}
Run Code Online (Sandbox Code Playgroud)

在graphiql中,有一个描述字段,但它总是说"自我描述".如何向架构添加描述?

javascript graphql apollo-server

51
推荐指数
3
解决办法
2万
查看次数

如何处理react-apollo中的删除操作

我有一个突变

mutation deleteRecord($id: ID) {
    deleteRecord(id: $id) {
        id
    }
}
Run Code Online (Sandbox Code Playgroud)

在另一个位置我有一个元素列表.

我可以从服务器返回更好的东西,我该如何更新列表?

更一般地说,在apollo/graphql中处理删除的最佳做法是什么?

javascript graphql apollo-server react-apollo

22
推荐指数
4
解决办法
1万
查看次数

createReadStream() 抛出 RangeError:上传文件时超出了最大调用堆栈大小

我正在尝试使用 Apollo Server 的Upload标量将文件直接发送到 S3。我的架构:

const { gql } = require('apollo-server-express')

module.exports = gql`

extend type Mutation {
  createPicture(
    name: String!
    picture: Upload!
  ): Picture!
}

type Picture {
  name: String!
  picture: String!
}
`
Run Code Online (Sandbox Code Playgroud)

解析器:

const { combineResolvers } = require('graphql-resolvers')
const isAuthenticated = require('./auth')
const { uploadPhoto } = require('../services/picture')

module.exports = {
  Mutation: {
    createPicture: combineResolvers(
      isAuthenticated,
      async (
        parent,
        { name, picture = null },
        { models, me }
      ) => {
        const { …
Run Code Online (Sandbox Code Playgroud)

node.js graphql apollo-server express-graphql

18
推荐指数
3
解决办法
8816
查看次数

使用makeRemoteExecutableSchema拼接安全订阅

我们已经实现了模式拼接,其中GraphQL服务器从两个远程服务器获取模式并将它们拼接在一起.当我们只使用Query和Mutations时,一切都运行正常,但现在我们有一个用例,我们甚至需要缝合订阅,远程模式已经通过它实现了auth.

我们很难搞清楚如何通过网关将connectionParams中收到的授权令牌从客户端传递到远程服务器.

这就是我们如何反省架构:

API网关代码:

const getLink = async(): Promise<ApolloLink> => {
const http = new HttpLink({uri: process.env.GRAPHQL_ENDPOINT, fetch:fetch})

const link = setContext((request, previousContext) => {
    if (previousContext
        && previousContext.graphqlContext
        && previousContext.graphqlContext.request
        && previousContext.graphqlContext.request.headers
        && previousContext.graphqlContext.request.headers.authorization) {
        const authorization = previousContext.graphqlContext.request.headers.authorization;
        return {
            headers: {
                authorization
            }
        }
    }
    else {
        return {};
    }
}).concat(http);

const wsLink: any = new WebSocketLink(new SubscriptionClient(process.env.REMOTE_GRAPHQL_WS_ENDPOINT, {
    reconnect: true,
    // There is no way to update connectionParams dynamically without resetting connection
    // connectionParams: () => …
Run Code Online (Sandbox Code Playgroud)

apollo graphql apollo-server graphql-subscriptions graphql-tools

17
推荐指数
1
解决办法
759
查看次数

apollo server 和 express-graphql 有什么区别

我想构建一个应用程序并推荐使用 GraphQl 作为 API,

我不确定选择哪个平台以及有什么区别。

apollo server vs express-graphql
Run Code Online (Sandbox Code Playgroud)

我也需要在项目中使用 TypeScript。任何好主意将不胜感激。

node.js graphql apollo-server

17
推荐指数
3
解决办法
1万
查看次数

“抽象类型 X 必须在运行时解析为具有值的字段 Query.user 的对象类型

这是我的代码

模式

gql`
  type Query {
    user: X!
  }
  type User {
    name: String!
  }
  type Time {
    age: Int!
  }
  union X = User | Time
`;
Run Code Online (Sandbox Code Playgroud)

解析器

{
  X: {
    __resolveType: obj => {
      if (obj.name) return { name: "Amasia" };
      if (obj.age) return { age: 70 };
      return null;
    }
  },
  Query: {
    user: () => {
      return {
        name: "Amasia"
      };
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

要求

query {
user{
  ... on User {
    name
  }
  ... on …
Run Code Online (Sandbox Code Playgroud)

node.js graphql-js apollo-server

17
推荐指数
1
解决办法
5669
查看次数

如何在单个查询中组合突变和查询?

我有一个操作getFoo要求用户通过身份验证才能访问资源。

用户使用突变进行身份验证authenticate,例如

mutation {
  authenticate (email: "foo", password: "bar") {
    id
  }
}
Run Code Online (Sandbox Code Playgroud)

当用户通过身份验证时,会发生两件事:

  1. 请求上下文中包含身份验证详细信息
  2. 一个cookie被创建

但是,我想将身份验证和getFoo方法调用结合到一个请求中,例如

mutation {
  authenticate (email: "foo", password: "bar") {
    id
  }
}
query  {
  getFoo {
    id
  }
}
Run Code Online (Sandbox Code Playgroud)

后者产生语法错误。

有没有办法将突变与查询结合起来?

node.js graphql apollo-server

15
推荐指数
1
解决办法
5266
查看次数

当没有数据返回时,GraphQL 突变返回类型应该是什么?

我有一个 Apollo GraphQL 服务器,我有一个删除记录的突变。此更改接收资源的 UUID,调用 REST(Ruby on Rails)API,当删除成功时,该 API 仅返回成功的 HTTP 代码和空正文(204 无内容)以及带有错误的 HTTP 错误代码删除不起作用时的消息(404 或 500,典型的 REST 删除端点)。

在定义 GraphQL 突变时,我必须定义突变返回类型。突变返回类型应该是什么?

input QueueInput {
  "The queue uuid"
  uuid: String!
}


deleteQueue(input: QueueInput!): ????????
Run Code Online (Sandbox Code Playgroud)

我可以使它与几种不同类型的返回(布尔值、字符串等)一起工作,但我想知道什么是最佳实践,因为我尝试过的返回类型都没有感觉是正确的。我认为重要的是,在调用突变后,在客户端我有一些关于如果事情进展顺利(API 返回 204 不是内容)或如果发生一些错误(API 返回 404 或 500)发生了什么的信息,并且最好有一些关于错误。

javascript rest apollo graphql apollo-server

15
推荐指数
1
解决办法
8205
查看次数

如何在apollo graphql server中创建嵌套解析器

鉴于以下apollo服务器graphql架构,我想将它们分解为单独的模块,因此我不希望根查询架构下的作者查询...并希望它分开.所以我在添加到根查询之前添加了另一个名为authorQueries的层

type Author {
    id: Int,
    firstName: String,
    lastName: String
}  
type authorQueries {
    author(firstName: String, lastName: String): Author
}

type Query {
    authorQueries: authorQueries
}

schema {
    query: Query
}
Run Code Online (Sandbox Code Playgroud)

我尝试了以下内容..您可以看到在指定作者函数之前,authorQueries已添加为另一个图层.

Query: {
    authorQueries :{
        author (root, args) {
            return {}
       }
    }
}
Run Code Online (Sandbox Code Playgroud)

在Graphiql中查询时,我还添加了额外的图层..

{
    authorQueries {
        author(firstName: "Stephen") {
            id
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我收到以下错误.

"message": "Resolve function for \"Query.authorQueries\" returned undefined",

graphql apollo-server apollostack

14
推荐指数
2
解决办法
1万
查看次数

具有动态键的对象的Apollo/GraphQL字段类型

假设我的graphql服务器想要获取以下数据作为JSON,其中person3person5是一些id:

"persons": {
  "person3": {
    "id": "person3",
    "name": "Mike"
  },
  "person5": {
    "id": "person5",
    "name": "Lisa"
  }
}
Run Code Online (Sandbox Code Playgroud)

问题:如何使用apollo创建模式类型定义?

密钥person3person5这里是动态生成的,具体取决于我的查询(即area在查询中使用).因此,在其他时间我可能会person1,person2,person3返回.如您所见persons,不是Iterable,因此以下不能用作我用apollo做的graphql类型定义:

type Person {
  id: String
  name: String
}
type Query {
  persons(area: String): [Person]
}
Run Code Online (Sandbox Code Playgroud)

persons对象中的键可能总是不同的.

当然,一种解决方案是将传入的JSON数据转换为使用数组persons,但是没有办法处理数据吗?

apollo graphql apollo-server

13
推荐指数
1
解决办法
6609
查看次数