1.54.0 • Published 4 years ago

cosmic_graphql v1.54.0

Weekly downloads
-
License
ISC
Repository
-
Last release
4 years ago

Running Graphql Locally

Example .env file:

POSTGRES=postgres
POSTGRES_DB=cosmic_graphql
POSTGRES_PASSWORD=nextleveldev_3
POSTGRES_USER=cosmic_graphile
POSTGRES_SCHEMA=cosmic_public

GIT_REF=master
USE_PGHOST=true
OSX_RUNTIME=true

PRIVATE_TOKEN=mygitlabprivatetoken

Make sure you are logged into the docker registry, using your personal access token as the password.

docker login registry.gitlab.app

Run Application

  1. npm install
  2. npm start to pull, build, and run prebuilt-db and cosmic-graphql containers locally
  3. If things seem broken, try npm run clean before npm start

Logs

  • docker-compose logs -f postgres to watch database logs
  • docker-compose logs -f cosmic-graphql to watch graphql logs

When the graphql logs say Schema finally generated successfully then the application is ready.

Updating Locally

  1. git pull to update to latest graphql-server
  2. npm run restart to rebuild and restart application if its already running, or just npm start if not already running
  3. Once application is ready, npm run migrate to update database schema to latest version

Emulating production

  • Ensure the project path to the cosmic-web project is accurate in the docker-compose.with-web.yml file.
# pull and start the pre-built database

npm start
docker-compose logs -f cosmic-graphql

Once the application is listening for connections, build and run the front-end:

docker-compose -f docker-compose.yml -f docker-compose.with-web.yml build
docker-compose -f docker-compose.yml -f docker-compose.with-web.yml up

Create and test GRAPHQL queries and mutations

# create a working branch

git checkout -b slangin-some-sql

# create and edit some sql functions

vim my-sweet-queries.sql

# load your new queries into the database

sh scripts/loadsql.sh my-sweet-queries.sql

If grahpql-server is already running, your queries should be loaded automatically. Snake_case function names will be converted to camelCase. For example the following function would be queried like { searchUsersByAuid(auidQuery: "...") { nodes { auid, email, ... }}}:

create or replace function cosmic_public.search_users_by_auid(
  auid_query  text
) returns setof cosmic_public.users
as $$
  select * from cosmic_public.users
  where auid like '%' || auid_query || '%';
$$ language sql stable;

By using sql features like create or replace you'll ensure that running loadsql.sh will not fail the second time around when you make changes because the function already exists.

Before pushing and MRing your new SQL, you should copy it into an appropriate file under the postgres directory and try to follow the conventions established by other functions in there.

For example you would not commit your my-sweet-queries.sql file in the example above, instead you'd delete that file and copy the function into postgres/03-migrations/001-search-users-by-auid.sql once you're certain it's working. As a final test you should make sure your function still works in its new location and regenerate the graphql schema files:

npm run clean
npm start

# wait for postgres to listen for connections
# test your graphql function and make sure it works
# then regenerate the schema before committing

npm run schema:generate