Comments (15)
Maybe helpful to others,
import fs from 'fs';
import path from 'path';
import { createGraphqlSchema } from 'postgraphql';
import 'postgraphql/dist/promisify';
import { graphql } from 'graphql';
import { introspectionQuery } from 'graphql/utilities';
(async () => {
const schema = await createGraphqlSchema('postgres://localhost:5432/your_db', 'your_schema')
.catch(err => console.error(err));
const result = await graphql(schema, introspectionQuery)
.catch(err => console.error(err));
if (result.errors) {
console.error('ERROR introspecting schema: ', result.errors);
} else {
fs.writeFileSync(
path.join(__dirname, '../schema.json'),
JSON.stringify(result, null, 2)
);
}
})().catch(err => console.error(err));
Lessons learned, dont forget your catch handlers …
from crystal.
Yep, ok, maybe time to migrate to pg-promise
. I thought importing postgraphql
also brought in the promisfy module. Oops 😊
from crystal.
Thank you @ferdinandsalis 😊
Use the introspection query here to get the schema.json
file.
As for #49, it's done in its most basic form. There is some mutation stuff and implementing node for viewer which still is yet to be done. If the basic form is enough for you, I can merge it and we can work out the intricacies later.
from crystal.
@calebmer thanks, yes do merge, however to be honest I am not sure if the basic form is enough for me. I have just started using postgresql and relay. I will give it a try.
from crystal.
@calebmer is this what you had in mind? I have tried and it returns an introspection error (see bottom).
import fs from 'fs';
import path from 'path';
import { createGraphqlSchema } from 'postgraphql';
import { graphql } from 'graphql';
import { introspectionQuery, printSchema } from 'graphql/utilities';
(async () => {
const schema = await createGraphqlSchema('postgres://localhost:5432', 'some_schema')
const result = await (graphql(schema, introspectionQuery));
if (result.errors) {
console.error(
'ERROR introspecting schema: ',
JSON.stringify(result.errors, null, 2)
);
} else {
fs.writeFileSync(
path.join(__dirname, '../schema.json'),
JSON.stringify(result, null, 2)
);
}
})();
the contents of result.errors
:
ERROR introspecting schema: [
{}
]
Thanks for your help.
from crystal.
I have tried the same with your forum_example. I get the same nondescript error. Might just be me ofcourse 😉.
from crystal.
Here is the gist of the schema output that I logged from the forum example —https://gist.github.com/ferdinandsalis/2215fb51378f2a3f25b458ac24884392
from crystal.
I'd recommend trying to run the introspection query either in GraphiQL, or directly send a request to the HTTP endpoint. There is some context values that might be missing here causing the query to fail.
This is not a use case I was originally planning to support, I'd have to look deeper at what's causing things to fail.
That said, here's some tips for debugging 😉
- Don't stringify
result.errors
when you log it to the console. There may be some fields the JSON serialization is stripping away. - Look here and here for an example of how we use
createGraphqlSchema
internally.
from crystal.
Thanks again. Hopefully I will make it work
from crystal.
I have added a catch handler to the promise returned from createGraphqlSchema
function and got the following error: [TypeError: _pg2.default.connectAsync is not a function]
. So I guess the pg instance is not promisified in this case?!
from crystal.
@calebmer have you seen https://github.com/vitaly-t/pg-promise which provides a very nice promise based layer for node-postgres.
from crystal.
This was the problem #55 (comment). Importing promisify in the script solved the problem. Schema was created. However maybe it makes more sense to get the schema from the http endpoint as you pointed out. I will need to test that.
from crystal.
@calebmer if you migrate to pg-promise
take note of https://github.com/vitaly-t/pg-monitor.
from crystal.
My version using latest 'createPostGraphQLSchema' and async/await... This is a modified version of whats found at https://github.com/lvarayut/relay-fullstack
/* eslint-disable no-console */
import path from 'path';
import fs from 'fs';
import { graphql } from 'graphql';
import chalk from 'chalk';
import { introspectionQuery, printSchema } from 'graphql/utilities';
import config from '../config/environment';
import { createPostGraphQLSchema } from 'postgraphql';
const jsonFile = path.join(__dirname, '../data/schema.json');
const graphQLFile = path.join(__dirname, '../data/schema.graphql');
async function updateSchema() {
try {
const schema = await createPostGraphQLSchema(config.postgres.url, config.postgres.schema);
const json = await graphql(schema, introspectionQuery);
fs.writeFileSync(jsonFile, JSON.stringify(json, null, 2));
fs.writeFileSync(graphQLFile, printSchema(schema));
console.log(chalk.green('Schema has been regenerated'));
} catch (err) {
console.error(chalk.red(err.stack));
}
}
// Run the function directly, if it's called from the command line
if (!module.parent) updateSchema();
export default updateSchema;
from crystal.
A feature for auto-exporting the schema was added in 3.0.0
thanks to @MaienM 🎉
from crystal.
Related Issues (20)
- Postgraphile plugin connection filter export to schema functionality pull request
- Multiple pgServices yields 'Attempted to add a second codec named 'varchar[]''
- Polymorphic types non-nullable attributes not working
- Weird behavior when setting pgServices.name HOT 2
- `@ref` should allow specifying the `from` so single-table polymorphism can apply refs to a single type
- Raise a GraphQL.js PR to allow enum values to be a callback HOT 2
- Comments on computed column functions are not present in the resulting GraphQL schema HOT 3
- Functions that return a specific polymorphic type
- Extra metadata to handle `@ref` relations for non-nullable relations plugin HOT 1
- Ruru incorrectly templates array values as singular HOT 1
- Error: New error HOT 2
- Outdated `chalk` dependency breaks `Postgraphile` when running with `--disable-proto=delete` or using `Deno` HOT 3
- Stack traces are displayed in production and can't be disabled HOT 1
- Plans returning constant(true) throw a step mis-match error
- Enums not matched in postgraphile.tags.json5 HOT 4
- Forbid exploring steps' dependencies during planning HOT 4
- Error when trying to add a directive with any arguments via makeExtendSchemaPlugin HOT 3
- Ruru headers are not passed along with requests to `/graphql/stream` HOT 4
- Intended way to return a union like `union Result = User | UsernameTaken | ...` in v5? HOT 2
- Set bodySizeLimit for graphile v5 HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from crystal.