Jump to content

GraphQL for ProcessWire


dadish

Recommended Posts

14 hours ago, blynx said:

The confusion might be about this:

Normally in processwire templates you have to "make the fields public" by manually echoing data in a template (echo $page->title) - so actually for a guest user everything is hidden by default - though by permission actually authorized.

With this module - everything gets "unveiled" (to use another term here) automatically. This is what I meant by "public" and "private".

... am I right?

Ooh, that's right. Now I get what you mean. Thank you for clarifying that for me.

That's true, with this module it gets available to the public without echoing it explicitly. So you will have to setup extra permissions to make it closed to the guest user. This was the initial intention of this module really. The goal is to build a tool that will allow you to quickly bootstrap an AJAX api of your ProcessWire content to build SPA out of it. For cases like you guys describe, this module might have some drawbacks. But you could always cook your own GraphQL api and make it behave however you want. It's fairly easy after you learn a bit about it. Here is the library I use for this module.

  • Like 5
Link to comment
Share on other sites

in regard to the public / private part, what @blynx mentioned, especially the by PW default "private fields" simply needs to be covered via accessrights on fieldlevel, set by the dev who wants to use this module. There is no need for public/private endpoints in the module. Or I'm wrong?

If you build a site without that module, you are done by simply use display logic in your templatefiles to control output of fields. If you want to use the module, you additionally have to mimik the accessrights via PW fields access settings. Thats how I understand it by just reading this really valuable thread. :)

@Nurguly Ashyrov ^-^

  • Like 4
Link to comment
Share on other sites

5 minutes ago, horst said:

in regard to the public / private part, what @blynx mentioned, especially the by PW default "private fields" simply needs to be covered via accessrights on fieldlevel, set by the dev who wants to use this module. There is no need for public/private endpoints in the mosule. Or I'm wrong?

If you build a site without that module, you are done by simply use display logic in your templatefiles to control output of fields. If you want to use the module, you additionally have to mimik the accessrights via PW fields access settings. Thats how I understand it by just reading this really valuable thread. :)

That's right. That's the way it will work when module is done. Currently there is a support for template permissions. Support for Field permissions are on the way, I am working on it but it will take some time. For now you can limit the fields via module settings, I pushed support for legal fields two hours ago.

12 minutes ago, horst said:

Thumbs up from @horst ! Yeeeey :) I am a big fan of your modules!

  • Like 7
Link to comment
Share on other sites

2 hours ago, bernhard said:

hi @Nurguly Ashyrov 

this looks very interesting!

could you please provide a simple example how one would use graphql and/or your module with simple ajax requests? i guess this could be great to return data for https://datatables.net/ ?

Hey there @bernhard! I am happy people like it.

To make an AJAX request to your GraphQL api, you need to send a query variable that describes what you want. You should've seen how the query looks like in the first post of this thread. To send the query you can use any of your favorite AJAX libraries. Here is how it looks like in jQuery.

$.post(
  '/graphql/', // this is the url where your GraphQL api is exposed.
  {
    query: "{ skyscrapers { list { title } } }", // this is your query, this one requests the skyscrapers and title field for each of them.
  },
  function (res) { console.log(res); } // here you do whatever you want with the returned data
);

Or if you prefer to communicate with your api in application/json, you can do that too. Here is how it looks like in my favorite AJAX library - superagent.

superagent
  .post('/graphql/')
  .set('Content-Type', 'application/json')
  .send({
    query: "{ city { list { title } } }"
  })
  .then(function (res) {
	console.log(res.body);
  });

PLEASE NOTE:  When making requests to your api make sure the url is pointing where it is supposed to. The request will not work if you omit the trailing slash. With default settings the request to /graphql won't be processed by ProcessWire but instead redirected to /graphql/ and the ProcessGraphQL module will not receive the query parameter you sent. Same might happen with leading www. prefix. The request to http://example.com will be redirected to http://www.example.com which will also loose the query parameter in the middle.

  • Like 8
Link to comment
Share on other sites

thank you for the quick response! i got that now :)

hmmm... so you define your query on the client side? how can you make sure that people do not modify your queries in a way that you do not want? i read of your server-side restrictions regarding templates, but wouldn't it still be possible to modify the queries to some extend (like changing number of records to show, fields and so on). i'm thinking of someone maybe stealing content or creating his own json exports of my site's content...

  • Like 1
Link to comment
Share on other sites

7 minutes ago, bernhard said:

but wouldn't it still be possible to modify the queries to some extend (like changing number of records to show, fields and so on). i'm thinking of someone maybe stealing content or creating his own json exports of my site's content...

For some websites this might even be the goal. But I can understand your reasoning. It might be interesting to be able to use the module to create endpoints with a somewhat fixed queries. Once the experimental phase is over and it goes towards production queries could be moved from the frontend to the backend.

 

  • Like 5
Link to comment
Share on other sites

12 hours ago, bernhard said:

hmmm... so you define your query on the client side? how can you make sure that people do not modify your queries in a way that you do not want? i read of your server-side restrictions regarding templates, but wouldn't it still be possible to modify the queries to some extend (like changing number of records to show, fields and so on). i'm thinking of someone maybe stealing content or creating his own json exports of my site's content...

That will all depend on how you configure your api. If you wish this module can expose all pages in your website, including the system ones. Or you can restrict to some very limited data. There are more functionality to come,

but at this stage the module supports:

  • Template restrictions. You can choose which templates are enabled, but in addition it will conform to ProcessWire permissions. So you could limit access to certain templates only to certain user roles. You can enable access to only logged in users for example.
  • Field restrictions. Also supports ProcessWire permissions. Including template context permissions. Meaning, you can allow title to be viewable for one template and restrict for another.
  • Max limit support. Like the one you use in selectors. So users won't be able to get list of data for more than say 50 pages at a time.

and also many other security options are on the roadmap: 

  • Built in field restrictions. At it's current stage api gives access to page fields like children, parents, createdUser..., or there are path, size, basename for files and images fields. Those will be disabled and available as extras.
  • Query complexity limit. Currently you can build queries as deep as you want, to request ridiculous amounts of data. This will also be limited for only couple levels of complexity and you will be able to increase or decrease it.
  • Like 10
Link to comment
Share on other sites

@Nurguly Ashyrov

Hello Nurguly,

I was following this thread from day one and I am deeply impressed by the passion you are putting into your 'baby'. Especially your in-depth explanations helped me understand your concept and getting first results on my own. Very often talented programmers do not have the same skill in teaching others . But you definitely have.

Right now I am experimenting with your module to figure out if this could be a  method to be used in a PWA (progressive web application). Using the GraphQL channel I would reload just my 'new' data via the service worker . Therefore it wouldn't matter that this data is exposed to the public - it should go there anyhow.

 

  • Like 8
Link to comment
Share on other sites

On 2/5/2017 at 11:44 PM, Werner Pilnei said:

Hello Nurguly,

I was following this thread from day one and I am deeply impressed by the passion you are putting into your 'baby'. Especially your in-depth explanations helped me understand your concept and getting first results on my own. Very often talented programmers do not have the same skill in teaching others . But you definitely have.

Thank you @Werner Pilnei. I am excited because I like using ProcessWire :). I try to do my best in introducing this module to the community. GraphQL is very young standard and is not mainstream yet. I intentionally started this thread in the Pub section, to make sure this is not a module support page but more a discussion on GraphQL (as this new api standard by facebook) and ProcessWire. To talk about how they could fit with each other, what ways we could use it, the new ways to use ProcessWire and so on.

I personally never think about ProcessWire as a CMS. Though it is in fact a true CMS in its literal meaning, it is best at managing your content. But when people are introduced to ProcessWire it is presented as CMS and since the web is cursed by WordPress, people start using ProcessWire with wrong assumptions in their minds which result in negative impressions. I am generalizing here but when an average web developer hears CMS, she thinks it is a ready website with bunch of functionality baked in like tags, searching, blogging, commenting and so on.  Those functionalities become the evaluation criteria and when they see that there is no tags in ProcessWire they count that as one of the things ProcessWire is missing. They don't understand that tags are something ProcessWire shouldn't have, because they are used to see tags in a CMS.

I don't think that I am telling something new here. The community is well aware of this problem and the release of new site profile states that these problems are being addressed. But it doesn't have to be the only way. The modular architecture of ProcessWire allows us to extend it anyway we want, and this module is one of those attempts in presenting ProcessWire in different perspective. Even if it won't make much difference, I think we should keep trying and experimenting. Who knows what could come up along the way. I was only thinking about SPAs when creating this module. Never thought of PWA and usage with service workers like you approached it. Which is, by the way is great to hear. I hope there will be bunch of other ways people use it.

  • Like 19
Link to comment
Share on other sites

Install seems fine, both in 1st and 2sd attempts, no error msgs, but when Setup > graphQL this shows up:

 

Parse Error: syntax error, unexpected '^', expecting ']' (line 9 of /home/zota/www/pw-logica/site/modules/ProcessGraphQL/GraphiQL/build/static/js/main.059c7daf.js) 

Any clue?
thanks

  • Like 1
Link to comment
Share on other sites

6 hours ago, zota said:

Parse Error: syntax error, unexpected '^', expecting ']' (line 9 of /home/zota/www/pw-logica/site/modules/ProcessGraphQL/GraphiQL/build/static/js/main.059c7daf.js) 

Any clue?
thanks

Just to make sure. Is this a PHP error, or is it a JavaScript error in your browser console?

Could you also give some environment details. Which version of PHP, ProcessWire?

  • Like 1
Link to comment
Share on other sites

Hey @zota. I made some changes on how the GraphiQL assets are loaded in the admin interface. Before that it was a quick stupid hack, sorry. Now it is as it supposed to be loaded for Process modules. I don't know why I didn't do it from the start. Please remove the old version and install the latest from repository. The error you are getting will most likely go away.

  • Like 3
Link to comment
Share on other sites

@Nurguly Ashyrov, this is really awesome. 

My front-end work is mostly JS/SPA work these days and I learnt to love the “headless”/“api first“ approach of commercial CMSes like Contentful. So I often thought about what a perfect and easy to manage back-end ProcessWire would be with a solid HTTP-based api built right in to allow uncoupling actual front-end/ui parts like admin and user interfaces (as actual self-contained apps) from the CMS itself.

A module like yours makes a huge leap in this direction and will make ProcessWire much more interesting for a lot of front-end devs and e.g. as a flexible and fast to set up back-end for app prototyping/mvp development. A pity this didn’t exist 9 month ago!

  • Like 11
Link to comment
Share on other sites

This is a fantastic module! Thank you Nurguly!

Does this handle Language fields yet? For example, I have translated titles, summaries for an "article"  template, and I've allowed the article template, along with the "title" and "summary" fields in the module settings, but the the translatable fields are still not available through GraphiQL, even though the other non-translatable fields are. 

 

  • Like 2
Link to comment
Share on other sites

@Oliver, @bcartier Thanks guys. Glad you like it.

3 hours ago, bcartier said:

Does this handle Language fields yet? For example, I have translated titles, summaries for an "article"  template, and I've allowed the article template, along with the "title" and "summary" fields in the module settings, but the the translatable fields are still not available through GraphiQL, even though the other non-translatable fields are. 

No, Language fields are not supported yet. There will be support for them too. Maybe I will implement them this weekend. In any case I will let you know when they are available.

For those who are following this module, I make releases almost everyday. Sometimes I introduce new bugs but patch them as soon as I find them. There are bunch of features added since the first introduction. You can follow up the changes/additions in the changelog. Lots of parts are not documented yet but I will provide full documentation and possibly introduction video targeted to ProcessWire users, on what the module is about, how you can use it, along with some todo app tutorial.

But for now, feel free to play with it and provide some feedback either here or in issue tracker. It is always easier to make changes in the beginning.

  • Like 9
Link to comment
Share on other sites

On 10/02/2017 at 7:49 PM, Oliver said:

@Nurguly Ashyrov

A module like yours makes a huge leap in this direction and will make ProcessWire much more interesting for a lot of front-end devs and e.g. as a flexible and fast to set up back-end for app prototyping/mvp development. A pity this didn’t exist 9 month ago!

Ditto this. I have been following this module development with huge interest. I too develop SPA with VueJS and this module will be very useful.

Hopefully not a stupid question, but is it possible, for example, to get cropped images? Or more of a general question, can you manipulate the data before you fetch it? Here is an example of the JSON I generate ATM:

{
	"banners":
	[
		{
			"origin": {
				"description": "",
				"httpUrl": "http://www.mysite.localdev/site/assets/files/1/img.jpg",
				"width": 2500,
				"height": 1666
			},
			"thumb": {
				"httpUrl": "http://www.mysite.localdev/site/assets/files/1/img.400x400.jpg",
				"width": 400,
				"height": 400,
				"orientation": ""
			},
			"bp": {
				"400": {
					"httpUrl": "http://www.mysite.localdev/site/assets/files/1/img.400x300.jpg"
				},
				"800": {
					"httpUrl": "http://www.mysite.localdev/site/assets/files/1/img.800x600.jpg"
				},
				"1200": {
					"httpUrl": "http://www.mysite.localdev/site/assets/files/1/img.1200x900.jpg"
				}
			},
			"srcset": "http://www.mysite.localdev/site/assets/files/1/img.400x300.jpg 400w,http://www.mysite.localdev/site/assets/files/1/img.800x600.jpg 800w,http://www.mysite.localdev/site/assets/files/1/img.1200x900.jpg 1200w"
		}
	]
}

 

  • Like 2
Link to comment
Share on other sites

14 minutes ago, microcipcip said:

is it possible, for example, to get cropped images? Or more of a general question, can you manipulate the data before you fetch it?

No there isn't at the moment. There will be support for it of course. The way it will work is if the user has a view access to the image field she will be able to fetch all the available image information, including the thumbnail variations. For user to be able to create thumbnails herself, she will need an edit access for that field.

  • Like 4
Link to comment
Share on other sites

Hey @bcartier and everyone who is following. I implemented a basic language support. Nothing really changed, except now with LanguageSupport enabled in your ProcessWire app, the GraphQL api will return the content in whatever language the user is assigned.

In addition, when Language Support module is activated, there is a language field in your GraphQL api. So you can request the exact language you want. It looks like this.

{
  language(name: "de")
  basic_page{
    list{
      title
      summary
    }
  }
}

You need to put the language field on the top. Well, not exactly on top but just before fields that return translatable content, like title, headline, body etc. It's because GraphQL processes requested fields from top to bottom and it will not know what language you want till it gets to language field. Did you also know that in GraphQL you can query same field multiple times with aliases? Here, take a look at this

{
  basic_page_default: basic_page{
    list{
      title
      summary
    }
  }
  language(name: "de")
  basic_page_de: basic_page{
    list{
      title
      summary
    }
  }
}

Curious what will be the response? Try this with site-languages  profile and find out.

  • Like 10
Link to comment
Share on other sites

  • 1 month later...

Oh wow @Nurguly Ashyrov  this is an amazing effort. SO much potential usecases. Thanks for all your work.
Maybe and depending how you feel about it you could mention this modue in the modules section, I only accidently kind of stumbled upon it here in Offtopic > Pub. This deserves much wider recognition.

  • Like 1
Link to comment
Share on other sites

I am happy that you like it @Sebastian, thank you for your support. I started the thread here because I thought this would be more like a discussion on how GraphQL and ProcessWire could fit together and wanted to get some feedback first. But this thread quickly become this module's official place here in the ProcessWire forums. Also @teppo included the link to this thread as the "dedicated support forum thread" in the 143 issue of the weekly.pw (which I was flattered to see :)).

Now I don't really know how to go on with this thread. Should we abandon it and start new thread in the modules section? Or maybe this thread could be moved to modules section? What @moderators think of this?

Meanwhile, for those who are following this thread I wanted to mention that there are some additions in the dev branch, such as mutations that allows you to create/update pages and there is also support for FieldtypeMapMarker field. I stopped developing the module for some time because I thought that it needed a good testing before moving further with it and decided to built an SPA using this module, to see if there is something that need to be added or changed. But then I got carried away and started to make usage of third-party APIs such as Wikipedia and GoogleMaps. As a result the app does not make heavy usage of the ProcessGraphQL module, but it is still relevant to showcase the module's abilities. It is a US Skyscrapers app, duh... You can see it live here and the source code here (though I doubt that the code will interest you if you are not a React developer).

I was finished with this demo SPA just couple of days ago. Now I will be back to continue to work on this module again.

  • Like 11
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...