我在前端有一个非常基本的 graphql 突变,我将其发送到后端。我在 by 上使用此代码graphql-request作为指南。
对于原语它可以工作:
const mutation = gql`
mutation EditArticle($id: ID!, $title: String) {
editArticle(id: $id, title: $title) {
id
}
}
`
Run Code Online (Sandbox Code Playgroud)
现在我还希望能够更改有关文章的一些元数据,这些元数据存储在meta文章内的对象中:
...,
title: "Hello World",
meta: {
author: "John",
age: 32,
...
}
Run Code Online (Sandbox Code Playgroud)
所以我的问题是:当使用graphql-request从前端发出请求时,如何将非原始对象类型作为参数传递给突变?
我已经尝试过这样的事情:
const Meta = new GraphQLObjectType({
name: "Meta",
fields: () => ({
id: { type: GraphQLID },
name: { type: GraphQLString },
age ....
}),
})
const mutation = gql`
mutation EditArticle($id: ID!, $title: …Run Code Online (Sandbox Code Playgroud) javascript graphql graphql-js express-graphql graphql-mutation
我正在构建一个带有联合模式的 Apollo 网关 - 并且我有许多子图 - 每个子图都有自己的身份验证令牌(例如许多 REST API,每个用户在每个 REST API 的数据库中保存了自己的令牌)。
我正在为网关中的每个用户的每个 REST API 获取令牌,以减少每个子图的过载并检查网关级别的权限,但我正在努力解决如何将凭据从网关传递到每个子图的问题。
我遇到了这个答案,但是,在这里他正在构建自己serviceList,而我正在使用 Apollo Federation - 并且网关对象无法访问,serviceMap因为它在 Typescript 定义下是私有的 - 这是一种非常黑客的方式实现它的方法:
class RequestHander extends RemoteGraphQLDataSource {
willSendRequest({ request }: { request: GraphQLRequest }) {
// if request.http.url matches url of a service which you
// use, add api-key to headers, e.g.
if (request.http.url === 'http://localhost:3001') {
request.http.headers.set('api-key', <API_KEY>)
}
}
}
const main = () => {
const gateway = …Run Code Online (Sandbox Code Playgroud) 我们正在使用 NestJS-typescript 开发微服务,每个服务都公开一个 GraphQL 模式。为了公开单个图,我们也在 NestJS 中使用联合服务。
我试图与“@graphql-eslint/eslint-plugin”集成,并引发了一些奇怪的异常:
未知指令“@key”
这些错误已在 GitHub 问题后解决:https ://github.com/cjoudrey/graphql-schema-linter/issues/210
我根据 Apollo 文档创建了一个具有类型规范的新文件:https://www.apollographql.com/docs/federation/federation-spec/#federation-schema-specation
解决这个问题后,我又收到了一些错误,我不知道它们的含义以及如何解决它们:
解析错误:无法扩展类型“Query”,因为它未定义。您指的是“用户”吗?
无法扩展类型“Mutation”,因为它未定义。
未知类型“查询”。您指的是“用户”吗?
未知类型“Mutation”.eslint
这是我的架构:
extend type Query {
users: [User]
user(email: EmailAddress!): User
}
extend type Mutation {
createUser(userData: UserData!): User
}
type User @key(fields: "email") {
email: String!
name: String
}
input UserData {
email: String!
name: String
}
Run Code Online (Sandbox Code Playgroud)
有谁知道为什么我会收到这些错误?
我这里有这个代码
\nimport React from "react";\n\nimport { useQuery } from "@apollo/react-hooks";\nimport gql from "graphql-tag";\n\nconst FETCH_POSTS_QUERY = gql`\n {\n getMovies {\n id\n title\n }\n }\n`;\n\nfunction Home() {\n const { loading, data } = useQuery(FETCH_POSTS_QUERY);\n\n if (data) {\n console.log(data);\n }\n\n return (\n <div>\n <h1>Home</h1>\n </div>\n );\n}\n\nexport default Home;\n\nRun Code Online (Sandbox Code Playgroud)\n当我运行它时,我的控制台中出现很多错误。我不确定我\xe2\x80\x99m 做错了什么。
\n\n我可以看出这是由我认为 useQuery 的使用方式引起的。但我不确定出了什么问题。
\n谢谢!
\n使用Relay和GraphQL,假设我有一个返回查看器的模式,以及一个嵌入的相关文档列表.根查询(由片段组成)看起来像这样:
query Root {
viewer {
id,
name,
groups {
edges {
node {
id,
name,
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这将允许我显示用户以及所有关联组的列表.
现在让我们说我希望用户能够点击该列表项,并让它展开以显示与该特定列表项关联的注释.我应该如何重新构建我的中继路由查询,以便我可以收到这些评论?如果我向组边缘添加注释边缘,那么它不会获取所有组的注释吗?
query Root {
viewer {
id,
name,
groups {
edges {
node {
id,
name,
comments {
edges {
node {
id,
content
}
}
}
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
或者我应该更改路由查询以查找特定组?
query Root {
group(id: "someid"){
id,
name,
comments {
edges {
node {
id,
content
}
}
}
},
viewer {
id,
name,
groups { …Run Code Online (Sandbox Code Playgroud) 假设我有一个GraphQL类型Echo,它可以回显我用一些装饰查询的内容.另一方面,我有一个React组件,echo message传递给它,其中一些装饰由Echo类型决定.如何设置initialVariables的Echo组成部分?
我读了设置道具集initialVariables,但是这不起作用.我试过了componentDidMount,但那也行不通.
此中继操场显示消息未正确显示.
对于上下文,
// This component consumes `Echo` type
class Echo extends React.Component {
componentDidMount() {
let {relay, message} = this.props;
relay.setVariables({
message
});
}
render() {
let name = '';
if (this.props.echo) {
name = this.props.echo.name;
}
return (
<li>Message: {name}</li>
);
}
}
Echo = Relay.createContainer(Echo, {
// By default `message` is null
initialVariables: {
message: null
},
fragments: {
echo: …Run Code Online (Sandbox Code Playgroud) 例如,我有连接类型:
let usersType = new GraphQLObjectType({
name: 'Users',
description: 'users array',
fields: () => ({
array: {
type: userConnection,
description: 'all users',
args: connectionArgs,
searchFor: {
type: GraphQLString
},
resolve: (root, args) => {
return connectionFromArray(get(), args);
}
}
})
});
Run Code Online (Sandbox Code Playgroud)
在这种情况下,在查询中我只能指定(first,last,after,before)参数,但如果我需要传递一些额外的参数,如userName等,那该怎么办呢?
基本上我需要这样的东西:
query {
array (first: 1, userName: "name")
}
Run Code Online (Sandbox Code Playgroud)
在用户类型我可以处理如下请求:
resolve: (root, args) => connectionFromArray(get(args.userName), args.args)
Run Code Online (Sandbox Code Playgroud) 我有以下架构:
import {
GraphQLSchema,
GraphQLObjectType,
GraphQLInt,
GraphQLString
} from 'graphql';
let counter = 100;
const schema = new GraphQLSchema({
// Browse: http://localhost:3000/graphql?query={counter,message}
query: new GraphQLObjectType({
name: 'Query',
fields: () => ({
counter: {
type: GraphQLInt,
resolve: () => counter
},
message: {
type: GraphQLString,
resolve: () => 'Salem'
}
})
}),
mutiation: new GraphQLObjectType({
name: 'Mutation',
fields: () => ({
incrementCounter: {
type: GraphQLInt,
resolve: () => ++counter
}
})
})
})
export default schema;
Run Code Online (Sandbox Code Playgroud)
以下查询工作正常:
{counter, message}
Run Code Online (Sandbox Code Playgroud)
但是,mutation …
我有一个带有查询的Express-GraphQL API和一个可在GraphiQL中工作的突变,查询的单元测试有效,但突变的单元测试返回405错误。我的GraphQL模式如下:
type Subject {
type: String
}
type Category {
name: String
}
type SubjectCategories {
subject: Subject
categories: [Category]
}
type Query {
subjectCategories(subjectType: String!): SubjectCategories
}
type Mutation {
addSubjectCategory(subjectType: String! categoryName: String!): SubjectCategories
}
Run Code Online (Sandbox Code Playgroud)
为了简化这个问题,这些方法的实现只是回显:
echoGetSubjectCategory({subjectType}, context) {
const subject = new Subject(subjectType);
const category = new Category("blah");
const retval = {
subject,
categories: [category],
};
return retval;
}
echoMutateSubjectCategory({subjectType, categoryName}, context) {
const subject = new Subject(subjectType);
const category = new Category(categoryName);
const retval = …Run Code Online (Sandbox Code Playgroud) 我正在第一次测试Gatsby和GraphQl,并且正在尝试一个简单的示例。
我想通过GraphQl请求在布局中显示标题时出现此错误?:未捕获的TypeError:无法读取未定义的属性“站点”
这是我的布局:
import React from 'react'
import { graphql } from 'gatsby'
export default ({ children, data }) =>
<div style={{margin: 'auto', maxWidth: 760}}>
<h2>{data.site.siteMetadata.title}</h2>
{children}
<footer>
Copyright blablb abolajoa.
</footer>
</div>
export const query = graphql`
query LayoutQuery {
site {
siteMetadata {
title
}
}
}
`
Run Code Online (Sandbox Code Playgroud)
和我的gatsby-config.js:
module.exports = {
siteMetadata: {
title: `Hardcoders`
},
plugins: [
{
resolve: 'gatsby-plugin-typography',
options: {
pathToConfigModule: 'src/utils/typography.js'
}
},
]
}
Run Code Online (Sandbox Code Playgroud)
这是项目的配置:
"dependencies": {
"gatsby": "^2.0.0",
"gatsby-plugin-typography": "^2.2.0", …Run Code Online (Sandbox Code Playgroud) graphql ×10
graphql-js ×10
javascript ×3
reactjs ×3
relayjs ×3
apollo ×1
eslint ×1
gatsby ×1
jestjs ×1
nestjs ×1
node.js ×1
react-apollo ×1
typescript ×1