简体   繁体   English

使用JavaScript定义GraphQL查询/突变的聪明方法

[英]Clever way to define a GraphQL Query/Mutation with JavaScript

Maybe it's just me but I´m pretty much annoyed of defining my GraphQL Queries/Mutations with backticks. 也许只有我一个人,但是我很讨厌用反引号定义我的GraphQL查询/突变。 The problem here is that, if I have to apply some changes to the query string, it's pretty annoying to reformat the string inside the backticks. 这里的问题是,如果我必须对查询字符串进行一些更改,那么在反引号内重新格式化该字符串会很烦人。

Is there a clever way to define a more readable and maintainable GraphQL Query/Mutation with JavaScript? 有没有一种聪明的方法来定义一个更易读和可维护的GraphQL查询/ mutt使用JavaScript? It's also hard to find missing brackets when the query string is messed up. 当查询字符串混乱时,也很难找到缺少的括号。

I am doing like this right now: 我现在正在这样做:

import gql from 'graphql-tag';

export const fancyMutation = gql`
mutation($id: ID!)
{
  delete_something(id:$id) {
    id
    foo
    bar
  }
}
`;

You can also put your queries into their own files, eg with a .graphql or .gql file extension, and use the graphql-tag/loader to load the query from the file where you need it. 您还可以将查询放入自己的文件中,例如,使用.graphql.gql文件扩展名,并使用graphql-tag / loader从需要的文件中加载查询。

Given this query in its own file: 在自己的文件中给出此查询:

query CurrentUserForLayout {
  currentUser {
    login
    avatar_url
  }
}

With webpack you would need the following configuration: 使用webpack,您需要以下配置:

module: {
  rules: [
    {
      test: /\.(graphql|gql)$/,
      exclude: /node_modules/,
      loader: 'graphql-tag/loader',
    },
  ],
},

Then you could load it like that: 然后,您可以像这样加载它:

import React, { Component } from 'react';
import { graphql } from 'react-apollo';
import currentUserQuery from './currentUser.graphql';

class Profile extends Component { ... }
Profile.propTypes = { ... };

export default graphql(currentUserQuery)(Profile)

Update : 更新

You can also have multiple queries in one file: 您还可以在一个文件中包含多个查询:

query MyQuery1 {
  ...
}

query MyQuery2 {
  ...
}

You can then load them like this: 然后可以像这样加载它们:

import { MyQuery1, MyQuery2 } from 'query.gql'

Other than that I don't know about other options to define queries inline. 除此之外,我不知道其他用于内联定义查询的选项。 The backticks aren't graphql specific. 反引号不是特定于graphql的。 It just uses the es6 template tags like eg styled components does to define inline css. 它仅使用es6模板标签 ,例如样式化组件确实定义了内联css。 Your only option is to get an IDE that can recognize graphql queries to provide editing help and code highlighting. 您唯一的选择是获得一个可以识别graphql查询的IDE,以提供编辑帮助和代码突出显示。 IntelliJ Editors (IDEA, WebStorm, PyCharm) have a plugin for that like @Victor Vlasenko pointed out. IntelliJ编辑器(IDEA,WebStorm,PyCharm)都有一个类似于@Victor Vlasenko指出的插件。

JS GraphQL WebStorm plugin starting from version 1.4.1 supports embedded GraphQL strings. 从1.4.1版开始的JS GraphQL WebStorm插件支持嵌入式GraphQL字符串。

From the WebStorm menu select File -> Settings -> Plugins -> Browse repositories and find JS GraphQL plugin. 从WebStorm菜单中,选择File -> Settings -> Plugins -> Browse repositories然后找到JS GraphQL插件。 Check that you have at least version 1.4.1 of this plugin installed. 检查您是否已至少安装此插件的1.4.1版本。

When installed it should highlight braces and let you reformat GraphQL inside string by selecting code block and choosing Code -> Reformat code from IDE menu. 安装后,它应突出显示花括号,并允许您通过选择代码块并从IDE菜单中选择Code -> Reformat code ,在字符串内重新格式化GraphQL。

This plugin project page is located here: 此插件项目页面位于此处:

https://github.com/jimkyndemeyer/js-graphql-intellij-plugin https://github.com/jimkyndemeyer/js-graphql-intellij-plugin

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM