Help us understand the problem. What is going on with this article?

GraphQLでレスポンスにCopyright表示を追加

More than 1 year has passed since last update.

やりたいこと=GraphQLのレスポンスにdataだけじゃなく説明とかいろいろ入れたい

Hiroyuki_OSAKIです。
GraphQLを使ってオープンデータを公開しようとしたら、オープンデータでもCopyright表示は必須、という条件をよく見ます。
そうするとどこに書かなければいけないのかというと、やはりAPIの返り値しか思いつきません。
でもGraphQL APIの返り値の形式はデフォルトではdataしか入っていません。

たとえばdata以外にmetaという要素をデフォルトで追記して、その中にcopyrightを入れるのはどうか。

{
  "data": {
    "location": {
      "Japanese": "高知市",
      "Prefecture": "Kōchi",
      "Country": "Japan"
    }
  },
  "meta": { //ここを追加したい
    "data_origin": "Wikipedia",
    "source_url": "https://en.wikipedia.org/wiki/List_of_cities_in_Japan",
    "licence_type": "https://en.wikipedia.org/wiki/Wikipedia:Text_of_Creative_Commons_Attribution-ShareAlike_3.0_Unported_License"
  }
}

どうやれば説明などメタデータを追加できるのでしょうか。

返り値はformatResponseで変更できる

formatResponseでメタデータ(コピーライトなど)を入れてみましょう。
apollo-serverやgraphql-yogaを使えば、server.startのオプションにformatResponseを入れるとよいです。

index.ts
  import { GraphQLServer } from 'graphql-yoga'
  const typeDefs = 'schema.graphql';
  const resolvers = { 
//略
  };

  const formatResponse = (response:any) => {
    var meta = {
      data_origin: "Wikipedia",
      source_url: "https://en.wikipedia.org/wiki/List_of_cities_in_Japan",
      licence_type: "https://en.wikipedia.org/wiki/Wikipedia:Text_of_Creative_Commons_Attribution-ShareAlike_3.0_Unported_License"
    }
    return {
      ...response,
      meta
    }
  }

  const server = new GraphQLServer({ typeDefs, resolvers })
  server.start({port: PORT, formatResponse}, () =>
    console.log(`Your GraphQL server is running now ...`),
  )

結果

お!できました!

{
  "data": {
    "location": { //ここには任意のデータが出ます
      "Japanese": "高知市",
      "Prefecture": "Kōchi",
      "Country": "Japan"
    }
  },
  "meta": { //ここから追加したメタデータが表示。
    "data_origin": "Wikipedia",
    "source_url": "https://en.wikipedia.org/wiki/List_of_cities_in_Japan",
    "licence_type": "https://en.wikipedia.org/wiki/Wikipedia:Text_of_Creative_Commons_Attribution-ShareAlike_3.0_Unported_License"
  }
}
Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away