#graphql-schema #query #generate #jddf #jddf-codegen #jddf-fuzz

app graphql-to-jddf

从GraphQL模式生成JDDF模式

1个不稳定版本

0.1.0 2020年1月20日

#2330数据库接口

MIT 协议

30KB
663

graphql-to-jddf

graphql-to-jddf 从GraphQL模式生成JDDF模式。一些用例包括

  • 从GraphQL生成类型(通过结合使用 graphql-to-jddfjddf-codegen),
  • 从GraphqL生成假数据(通过结合使用 graphql-to-jddfjddf-fuzz),
  • 在GraphQL服务器外部验证GraphQL数据(通过结合使用 graphql-to-jddf 和任何JDDF实现)。

例如,graphql-to-jddf 可以将GraphQL模式

schema {
  query: Query
}

type Query {
  foo: String!
}

转换为JDDF模式

{
  "definitions": {
    "Query": {
      "properties": {
        "foo": {
          "type": "string"
        }
      }
    }
  },
  "ref": "Query"
}

使用方法

有关详细信息,请参阅 graphql-to-jddf --help,但基本使用方法是,以GitHub的GraphQL API为例

graphql-to-jddf \
  --http-endpoint=https://api.github.com/graphql \
  --http-bearer-token=YOUR_BEARER_TOKEN_HERE

graphql-to-jddf 基于 内省的GraphQL模式,而不是 .graphql 模式文件。如果您不是通过HTTP/HTTPS提供GraphQL,或者您不是使用基于Bearer的认证策略,您也可以简单地传递标准内省GraphQL查询的输出作为输入给 graphql-to-jddf

您可以在本仓库的 src/graphql/introspection_query.graphql 中找到标准内省GraphQL查询。

例如,如果您将 introspection_query.graphql 粘贴到 https://graphql.net.cn/swapi-graphql/,即标准GraphQL服务器演示实现,然后将结果复制到某个文件 star_wars.json 中,然后可以运行

cat star_wars.json | graphql-to-jddf

以生成JDDF模式。当然,您也可以直接运行

graphql-to-jddf --http-endpoint=https://swapi-graphql.netlify.com/.netlify/functions/index

以获得相同的结果。

示例和注意事项

以下是 graphql-to-jddf 生成的JDDF模式的一些示例。本节中显示的GraphQL .graphql 模式是在本仓库的 [example_graphql_server][example-graphql-server] 中托管。

更复杂的GraphQL模式

GraphQL模式

schema {
  query: Query
}

type Query {
  scalars: Scalars!
  listOfScalars: [Scalars!]!
  deepListOfScalars: [[[Scalars!]!]!]!
}

type Scalars {
  a: String!
  b: ID!
  c: Boolean!
  d: Int!
  e: Float!
}

变为

{
  "definitions": {
    "Query": {
      "properties": {
        "listOfScalars": {
          "elements": {
            "ref": "Scalars"
          }
        },
        "deepListOfScalars": {
          "elements": {
            "elements": {
              "elements": {
                "ref": "Scalars"
              }
            }
          }
        },
        "scalars": {
          "ref": "Scalars"
        }
      }
    },
    "Scalars": {
      "properties": {
        "d": {
          "type": "int32"
        },
        "b": {
          "type": "string"
        },
        "e": {
          "type": "float64"
        },
        "a": {
          "type": "string"
        },
        "c": {
          "type": "boolean"
        }
      }
    }
  },
  "ref": "Query"
}

嵌套的限制

GraphQL 的反射系统不允许获取任意深度的数组或非空值。因此,在某个点上 graphql-to-jddf 将“触底”,并简单地输出一个空的(通配符)模式。

例如,这个 GraphQL 模式

schema {
  query: Query
}

type Query {
  deep: [[[String!]!]!]!
}

变为

{
  "definitions": {
    "Query": {
      "properties": {
        "deep": {
          "elements": {
            "elements": {
              "elements": {
                "type": "string"
              }
            }
          }
        }
      }
    }
  },
  "ref": "Query"
}

但是这个 GraphQL 模式达到了最大深度

schema {
  query: Query
}

type Query {
  deep: [[[[String!]!]!]!]!
}

因此变成了(注意缺少 {"type": "string"})

{
  "definitions": {
    "Query": {
      "properties": {
        "deep": {
          "elements": {
            "elements": {
              "elements": {
                "elements": {}
              }
            }
          }
        }
      }
    }
  },
  "ref": "Query"
}

联合和接口的限制

graphql-to-jddf 对于 GraphQL 接口和联合的输出并不理想

schema {
  query: Query
}

type Query {
  union: U
  interface: I
}

union U = A | B | C

interface I {
  x: String!
}

type A {
  x: String!
}

type B {
  x: String!
}

type C {
  x: String!
}

变为

{
  "definitions": {
    "Query": {
      "properties": {},
      "optionalProperties": {
        "union": {
          "ref": "U"
        },
        "interface": {
          "ref": "I"
        }
      }
    },
    "I": {},
    "U": {},
    "A": {
      "properties": {
        "x": {
          "type": "string"
        }
      }
    },
    "B": {
      "properties": {
        "x": {
          "type": "string"
        }
      }
    },
    "C": {
      "properties": {
        "x": {
          "type": "string"
        }
      }
    }
  },
  "ref": "Query"
}

这是一个已知限制。如果您有从 unioninterface 输出特定类型的 JDDF 模式的用例,请提交一个 GitHub 工单。

依赖

~11–15MB
~299K SLoC