我有一个 Apollo GraphQL 服务器与 API 通信,返回响应的大致结构如下:
{
"pagination": {
"page": 1,
// more stuff
},
sorting: {
// even more stuff
},
data: [ // Actual data ]
}
Run Code Online (Sandbox Code Playgroud)
这个结构将在我广泛使用的这个 API 的几乎所有响应中共享。data大多数情况下将是一个数组,但也可以是一个对象。
我如何以有效的方式编写它,以便我不必在模式中的每个数据类型上重复所有这些pagination和sorting字段?
非常感谢!
在这一刻我有这样的事情
return this._http.get('https://api.github.com/users/' + this.username + '/repos?client_id=' + this.client_id + '&client_secret=' + this.client_secret)
.map(res => res.json());
Run Code Online (Sandbox Code Playgroud)
获取所选用户的存储库列表(不使用 GraphQL)。
除了使用 GraphQL 之外,如何获取问题列表?这是 GitHub API 文档的示例:
query {
repository(owner:"octocat", name:"Hello-World") {
issues(last:20, states:CLOSED) {
edges {
node {
title
url
labels(first:5) {
edges {
node {
name
}
}
}
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
如何在 Angular 4 中实现它?
我的目标是遍历一个 graphql Java 文档对象并返回最大深度。
示例:深度 0
{
name
}
Run Code Online (Sandbox Code Playgroud)
示例:深度 1
{
viewer{
viewerId
}
}
Run Code Online (Sandbox Code Playgroud)
示例:深度 2
{
viewer{
albums{
albumId
}
}
}
Run Code Online (Sandbox Code Playgroud)
示例:深度 2。如您所见,两张专辑/歌曲都在同一个父“观众”下
{
viewer{
albums{
albumId
}
songs{
songId
}
}
}
Run Code Online (Sandbox Code Playgroud)
示例:深度 3
{
viewer{
albums{
tracks{
trackId
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我已经编写了基本代码来遍历它,但我的代码不适用于第二个版本的 depth = 2。它返回 depth = 3 而不是 2。原因是因为它在同一个父项下计数两次。本质上的逻辑是这样的:depth = depth + 1 每当一个字段有子项时。
import graphql.language.Document;
import graphql.language.Node;
import graphql.language.OperationDefinition;
public int checkDepthLimit(String query) {
Document document;
try {
document = documentParser.parseDocument(query); …Run Code Online (Sandbox Code Playgroud) 在我的 Graphene-Django 项目中,我有这样的结构:
项目级别:
schema.py
Run Code Online (Sandbox Code Playgroud)
应用级别:
schema.py
queries.py
mutations.py
Run Code Online (Sandbox Code Playgroud)
这很有效,但查询文件已经变得非常大。有没有办法将类 Query拆分为多个类和/或多个文件?
罗伯特
我是 graphQL 石墨烯(python)的新手。我想知道是否可以使用石墨烯创建订阅根类型。谢谢
graphql graphene-python graphql-subscriptions graphql-mutation
我按照本教程使用 Amplify 部署了一个 AWS AppSync GraphQL 终端节点:
https://aws-amplify.github.io/docs/js/api#amplify-graphql-client
我用 Node.js 和 TypeScript 创建了一个 Lambda 函数来查询数据:
import { APIGatewayEvent, Callback, Context, Handler } from 'aws-lambda';
import Amplify, { API, graphqlOperation } from "aws-amplify";
import * as queries from './src/graphql/queries';
import * as mutations from './src/graphql/mutations';
import { CreateBlogInput } from './src/API';
import aws_config from "./src/aws-exports";
Amplify.configure(aws_config);
export const list: Handler = async (event: APIGatewayEvent, context: Context, cb: Callback) => {
const allBlogs = await API.graphql(graphqlOperation(queries.listBlogs));
// this seems to be working …Run Code Online (Sandbox Code Playgroud) amazon-web-services aws-lambda graphql aws-appsync aws-amplify
因此,我创建了一堆突变和查询,并将它们拼接在一起,这些工作并希望将身份验证引入到组合中。我添加了一个 HTTP 标头“x-token”来保存我的登录令牌,以便能够删除他们的工作或用户本身等内容。
const getMe = async req => {
const token = req.headers['x-token'];
if (token) {
try {
return await jwt.verify(token, "notSoSecret");
} catch (e) {
throw new AuthenticationError(
'Your session expired. Sign in again.',
);
}
}
};
const server = new ApolloServer({
typeDefs: schema,
resolvers,
formatError: error => {
// remove the internal sequelize error message
// leave only the important validation error
const message = error.message
.replace('SequelizeValidationError: ', '')
.replace('Validation error: ', '');
return {
...error, …Run Code Online (Sandbox Code Playgroud) 我正在使用 GatsbyJS,这是我第一次使用 react 和 graphql。我想混合单个页面的内容以包含“博客文章”类型的内容和“repo-post”类型的内容,但我不知道如何将其添加到查询中以对其进行过滤。
此外; 我知道 frontmatter 是什么(re:markdown),但我不知道在这种情况下什么 templateKey 和“eq”。我不太了解我在看什么,不知道什么叫开始寻找解释。
export const pageQuery = graphql`
query IndexQuery {
allMarkdownRemark(
sort: { order: DESC, fields: [frontmatter___date] },
filter: { frontmatter: { templateKey: { eq: "blog-post" } }}
) {
edges {
node {
excerpt(pruneLength: 400)
id
fields {
slug
}
frontmatter {
title
templateKey
date(formatString: "MMMM DD, YYYY")
}
}
}
}
}
`
Run Code Online (Sandbox Code Playgroud) graphql我在我的应用程序上使用 Apollo Client 作为客户端next.js,以下是为我创建客户端的函数:
let client: ApolloClient<any>;
export const __ssrMode__: boolean = typeof window === "undefined";
export const uri: string = "http://localhost:3001/graphql";
const createApolloClient = (): ApolloClient<any> => {
return new ApolloClient({
credentials: "include",
ssrMode: __ssrMode__,
link: createHttpLink({
uri,
credentials: "include",
}),
cache: new InMemoryCache(),
});
};
Run Code Online (Sandbox Code Playgroud)
令人惊讶的是,当我对 graphql 服务器进行更改时,我能够设置 cookie,但是我无法从客户端获取 cookie。可能是什么问题?
我正在为我用 Go 编写的 BE 的 GraphQL 查询开发一个解析器函数。在解析器中,我有想要更新的用户数据,使用包含多个可能的更新属性的输入值。
在 JavaScript 中,这可以通过解构(伪)快速完成:
const mergedObj = {...oldProps, ...newProps}
目前,我的解析器函数如下所示(使用 gqlgen 作为 GraphQL Go 解析器):
func (r *mutationResolver) ModifyUser(ctx context.Context, input *model.ModifyUserInput) (*model.User, error) {
id := input.ID
us, ok := r.Resolver.UserStore[id]
if !ok {
return nil, fmt.Errorf("not found")
}
if input.FirstName != nil {
us.FirstName = *input.FirstName
}
if input.LastName != nil {
us.LastName = *input.LastName
}
if input.ProfileImage != nil {
us.ProfileImage = input.ProfileImage
}
if input.Password != nil {
us.Password = …Run Code Online (Sandbox Code Playgroud) graphql ×10
apollo ×3
angular ×1
aws-amplify ×1
aws-appsync ×1
aws-lambda ×1
django ×1
express ×1
gatsby ×1
github-api ×1
go ×1
gqlgen ×1
graphql-java ×1
graphql-js ×1
java ×1
next.js ×1
node.js ×1
react-apollo ×1
reactjs ×1
recursion ×1
relay ×1
sequelize.js ×1
serverless ×1