Code Monkey home page Code Monkey logo

Comments (15)

ferdinandsalis avatar ferdinandsalis commented on May 22, 2024 3

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.

calebmer avatar calebmer commented on May 22, 2024 1

Yep, ok, maybe time to migrate to pg-promise. I thought importing postgraphql also brought in the promisfy module. Oops 😊

from crystal.

calebmer avatar calebmer commented on May 22, 2024

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.

ferdinandsalis avatar ferdinandsalis commented on May 22, 2024

@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.

ferdinandsalis avatar ferdinandsalis commented on May 22, 2024

@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.

ferdinandsalis avatar ferdinandsalis commented on May 22, 2024

I have tried the same with your forum_example. I get the same nondescript error. Might just be me ofcourse 😉.

from crystal.

ferdinandsalis avatar ferdinandsalis commented on May 22, 2024

Here is the gist of the schema output that I logged from the forum example —https://gist.github.com/ferdinandsalis/2215fb51378f2a3f25b458ac24884392

from crystal.

calebmer avatar calebmer commented on May 22, 2024

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.

ferdinandsalis avatar ferdinandsalis commented on May 22, 2024

Thanks again. Hopefully I will make it work ☺️. I would hate to go down my the original road rebuilding parts of postgraphql. Which seems just wrong … considering my expertise 😅

from crystal.

ferdinandsalis avatar ferdinandsalis commented on May 22, 2024

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.

ferdinandsalis avatar ferdinandsalis commented on May 22, 2024

@calebmer have you seen https://github.com/vitaly-t/pg-promise which provides a very nice promise based layer for node-postgres.

from crystal.

ferdinandsalis avatar ferdinandsalis commented on May 22, 2024

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.

ferdinandsalis avatar ferdinandsalis commented on May 22, 2024

@calebmer if you migrate to pg-promise take note of https://github.com/vitaly-t/pg-monitor.

from crystal.

gregpardo avatar gregpardo commented on May 22, 2024

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.

calebmer avatar calebmer commented on May 22, 2024

A feature for auto-exporting the schema was added in 3.0.0 thanks to @MaienM 🎉

from crystal.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.