Skip to content

Commit

Permalink
Enhancement/issue 270 greenwood configuration as data (#296)
Browse files Browse the repository at this point in the history
* Rfc/issue 115 build time data access (#269)

* graphql server working

* apollo client connect to apollo server

* connected header example using lit apollo

* todo

* todos

* query and client + server refactor

* schema refactoring

* clean up console logging

* alias all @greenwood/cli/data module imports

* avoid paramater destructuring

* graphql example in the header

* multiple schemas

* internal data sources documentation

* shelf refactor and children query integration

* refactor out ApolloQuery

* ability to intercept client.query calls

* basic semi-working implementation

* remove extra config from server context

* have puppeteer wait for graphql requests before returning content

* fix and add test cases for apollo

* merged resolvers not actually working

* multiple queries support

* everything working

* todos

* TODO tracking

* fix fallback apollo client fetch handling

* full test suite

* cache json test cases

* stablize test due to inconsistent data results ordering

* clean up deps

* todo cleanup

* remove forced client call in SSG mode for client

* represent graph through the schema

* updated data docs

* typos and grammer

* typos and community link fixes

* hello and graph queries working together

* config def and resolvers

* use real config data

* flesh out config schema and query

* remove theme file from config

* delete hello query example

* config unit tests

* rename mock graph

* formatting

* documentation
  • Loading branch information
thescientist13 authored Mar 15, 2020
1 parent 8d1529a commit ad9515b
Show file tree
Hide file tree
Showing 15 changed files with 271 additions and 64 deletions.
18 changes: 18 additions & 0 deletions packages/cli/src/data/queries/config.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
query {
config {
devServer {
port,
host
},
meta {
name,
rel,
content,
property,
value
},
publicPath,
title,
workspace
}
}
3 changes: 0 additions & 3 deletions packages/cli/src/data/queries/hello.gql

This file was deleted.

44 changes: 44 additions & 0 deletions packages/cli/src/data/schema/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const { gql } = require('apollo-server');

const getConfiguration = async (root, query, context) => {
return context.config;
};

// https://www.greenwoodjs.io/docs/configuration
const configTypeDefs = gql`
type DevServer {
port: Int,
host: String
}
type Meta {
name: String,
value: String,
content: String,
rel: String,
property: String
}
type Config {
devServer: DevServer,
meta: [Meta],
publicPath: String,
title: String,
workspace: String
}
extend type Query {
config: Config
}
`;

const configResolvers = {
Query: {
config: getConfiguration
}
};

module.exports = {
configTypeDefs,
configResolvers
};
18 changes: 0 additions & 18 deletions packages/cli/src/data/schema/hello.js

This file was deleted.

21 changes: 13 additions & 8 deletions packages/cli/src/data/schema/schema.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
// TODO merging resolvers not actually working, resolve as part of #21 or #270
const { makeExecutableSchema } = require('apollo-server-express');
const { helloTypeDefs, helloResolvers } = require('./hello');
const { configTypeDefs, configResolvers } = require('./config');
const { graphTypeDefs, graphResolvers } = require('./graph');

const mergedResolvers = Object.assign({}, {
Query: {
...configResolvers.Query,
...graphResolvers.Query
}
});

const schema = makeExecutableSchema({
typeDefs: [
graphTypeDefs,
helloTypeDefs
configTypeDefs,
graphTypeDefs
],
resolvers: Object.assign({},
graphResolvers,
helloResolvers
)
resolvers: [
mergedResolvers
]
});

module.exports = schema;
3 changes: 2 additions & 1 deletion packages/cli/src/data/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const schema = require('./schema/schema');
const createCache = require('./cache');

module.exports = (compilation) => {
const { graph, context } = compilation;
const { config, graph, context } = compilation;

// Create schema
const server = new ApolloServer({
Expand All @@ -22,6 +22,7 @@ module.exports = (compilation) => {
}

return {
config,
graph
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ describe('Build Greenwood With: ', function() {
const apolloScriptTags = Array.prototype.slice.call(scriptTags).filter(script => {
return script.getAttribute('data-state') === 'apollo';
});

const innerHTML = apolloScriptTags[0].innerHTML;

expect(apolloScriptTags.length).to.equal(1);
Expand Down
17 changes: 17 additions & 0 deletions packages/cli/test/unit/data/mocks/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const MOCK_CONFIG = {
config: {
devServer: {
port: 1984,
host: 'localhost'
},
meta: [
{ name: 'twitter:site', content: '@PrjEvergreen' },
{ rel: 'icon', href: '/assets/favicon.ico' }
],
publicPath: '/some-dir',
title: 'My App',
workspace: 'src'
}
};

module.exports = MOCK_CONFIG;
4 changes: 2 additions & 2 deletions packages/cli/test/unit/data/mocks/graph.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const GRAPH_MOCK = {
const MOCK_GRAPH = {
graph: [
{ mdFile: './index.md',
label: '37b24fd9f163e7b',
Expand Down Expand Up @@ -276,4 +276,4 @@ const GRAPH_MOCK = {
]
};

module.exports = GRAPH_MOCK;
module.exports = MOCK_GRAPH;
83 changes: 83 additions & 0 deletions packages/cli/test/unit/data/schema/config.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
const expect = require('chai').expect;
const MOCK_CONFIG = require('../mocks/config');
const { configResolvers } = require('../../../../src/data/schema/config');

describe('Unit Test: Data', function() {

describe('Schema', function() {

describe('Config', function() {
let config = {};

before(async function() {
config = await configResolvers.Query.config(undefined, {}, MOCK_CONFIG);
});

describe('Dev Server', function() {
const { devServer } = MOCK_CONFIG.config;

it('should have the expected devServer.port', function() {
expect(config.devServer.port).to.equal(devServer.port);
});

it('should have the expected devServer.host', function() {
expect(config.devServer.host).to.equal(devServer.host);
});
});

describe('Meta', function() {
const { meta } = MOCK_CONFIG.config;

it('should have the expected name meta in the first indexx', function() {
const nameMeta = config.meta[0];

expect(nameMeta.name).to.equal(meta[0].name);
});

it('should have the expected devServer.host', function() {
const nameMeta = config.meta[0];

expect(nameMeta.content).to.equal(meta[0].content);
});

it('should have the expected rel meta in the second index', function() {
const relMeta = config.meta[1];

expect(relMeta.rel).to.equal(meta[1].rel);
});

it('should have the expected devServer.host', function() {
const relMeta = config.meta[1];

expect(relMeta.content).to.equal(meta[1].content);
});
});

describe('Public Path', function() {
const { publicPath } = MOCK_CONFIG.config;

it('should have the expected publicPath', function() {
expect(publicPath).to.equal(config.publicPath);
});
});

describe('Title', function() {
const { title } = MOCK_CONFIG.config;

it('should have the expected title', function() {
expect(title).to.equal(config.title);
});
});

describe('Workspace', function() {
const { workspace } = MOCK_CONFIG.config;

it('should have the expected title', function() {
expect(workspace).to.equal(config.workspace);
});
});

});

});
});
24 changes: 4 additions & 20 deletions packages/cli/test/unit/data/schema/graph.spec.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,5 @@
/*
* Use Case
* Run Greenwood with empty config object and default workspace.
*
* Uaer Result
* Should generate a bare bones Greenwood build. (same as build.default.spec.js)
*
* User Command
* greenwood build
*
* User Config
* {}
*
* User Workspace
* Greenwood default (src/)
*/
const expect = require('chai').expect;
const GRAPH_MOCK = require('../mocks/graph');
const MOCK_GRAPH = require('../mocks/graph');
const { graphResolvers } = require('../../../../src/data/schema/graph');

describe('Unit Test: Data', function() {
Expand All @@ -28,7 +12,7 @@ describe('Unit Test: Data', function() {
let pages = [];

before(async function() {
pages = await graphResolvers.Query.graph(undefined, {}, GRAPH_MOCK);
pages = await graphResolvers.Query.graph(undefined, {}, MOCK_GRAPH);
});

it('should have 27 pages', function() {
Expand All @@ -51,7 +35,7 @@ describe('Unit Test: Data', function() {
let navigation = [];

before(async function() {
navigation = await graphResolvers.Query.navigation(undefined, {}, GRAPH_MOCK);
navigation = await graphResolvers.Query.navigation(undefined, {}, MOCK_GRAPH);
});

it('should have 4 children', function() {
Expand Down Expand Up @@ -91,7 +75,7 @@ describe('Unit Test: Data', function() {
let children = [];

before(async function() {
children = await graphResolvers.Query.children(undefined, { parent: 'getting-started' }, GRAPH_MOCK);
children = await graphResolvers.Query.children(undefined, { parent: 'getting-started' }, MOCK_GRAPH);
});

it('should have 8 children', function() {
Expand Down
1 change: 1 addition & 0 deletions www/components/shelf/shelf.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class Shelf extends LitElement {
console.log('response from the shelf (data.children)', response.data.children);

this.shelfList = list;
console.log('shelf list', this.shelfList);
}
}

Expand Down
Loading

0 comments on commit ad9515b

Please sign in to comment.