exogen / graphql-markdown

The easiest way to document your GraphQL schema.
MIT License
179 stars 55 forks source link

Error: Unknown directive "@entity" #79

Open pandichef opened 3 years ago

pandichef commented 3 years ago

I'm using thegraph.com's schema convention e.g., https://thegraph.com/docs/developer/create-subgraph-hosted#good-example. My schema looks like this:

type Item @entity {
  id: ID!
  var1: BigInt!
  var2: BigInt!
  name: String!
}

I get this error when I run npx graphql-markdown schema.graphql schema.md:

Error: Unknown directive "@entity".
Unknown type "BigInt". Did you mean "Int"?

Can this tool accommodate or should I look elsewhere for a solution? Ty

wagnerlduarte commented 2 years ago

Hey @pandichef! This problem happen because the graphql-markdown use the graphql to generate de markdown file. So, it need the typeDefs and directives to works well.

Do you need to include in your .graphql file these definitions manually how do you can see here, or make it programatically.

const { execSync } = require('child_process')
const fs = require('fs')

const argv = require('minimist')(process.argv.slice(2))
const { yourTypeDefs } = require('your-package')
const graphql = require('graphql')
const { loadFilesSync } = require('@graphql-tools/load-files')
const { makeExecutableSchema } = require('@graphql-tools/schema')
const { mergeTypeDefs } = require('@graphql-tools/merge')
const { stitchingDirectives } = require('@graphql-tools/stitching-directives')

const { allStitchingDirectivesTypeDefs } = stitchingDirectives()

const DEFAULT_GRAPHQL_PATH = './graphql/**/*.{gql,graphql}'
const DEFAULT_MERGED_PATH = './merged.graphql'
const DEFAULT_MARKDOWN_PATH = './schema.md'

const GRAPHQL_ARG = argv['graphql-path']
const MERGED_ARG = argv['merged-path']
const MARKDOWN_ARG = argv['markdown-path']

const GRAPHQL_PATH = GRAPHQL_ARG || DEFAULT_GRAPHQL_PATH
const MERGED_PATH = MERGED_ARG || DEFAULT_MERGED_PATH
const MARKDOWN_PATH = MARKDOWN_ARG || DEFAULT_MARKDOWN_PATH

const loadedFiles = loadFilesSync(GRAPHQL_PATH)

const typeDefs = mergeTypeDefs(loadedFiles)

const schema = makeExecutableSchema({
  typeDefs: [allStitchingDirectivesTypeDefs, typeDefs, yourTypeDefs],
})

const printedSchema = graphql.printSchema(schema)

fs.writeFileSync(MERGED_PATH, printedSchema)

execSync(
  `graphql-markdown ${MERGED_PATH} > ${MARKDOWN_PATH} && rm ${MERGED_PATH}`
)