Retrieve current XP server time by Guillotine API

Enonic version: 7.7.0
OS: Linux

Hello,

We have a headless application running on Enonic XP. We would like to use the XP server for time synchronization on client side.
Is there any change to select the server current time by GraphQL via Guillotine API?

Thank you for response
Pavel

Guillotine API is a headless content API. It is technically possible to create a content that would report server time, but would be awkward.
You could instead extend GraphQL schema

var schema = guillotineLib.createSchema({
        creationCallbacks: {
            'Query': function (context, params) {
                params.fields.getServerTime = {
                    type: graphQlLib.GraphQLString,
                    resolve: function (env) {
                        return Java.type('java.time.LocalDateTime').now()
                    }
                };
            }
        }
    }
);
1 Like

Hi Pavel,
You can customize a schema using creationCallbacks as in the example in the previous reply. More details you can find here

But in your case I suggest to implement ScriptBean helper which will return server time in your application.

Java:

public class ServerTimeHelper implements ScriptBean {
        @Override
        public void initialize( final BeanContext context )        
        {
        }        
        public long getTime() {
            return new Date().getTime();
        }
    }    

JS:

    var bean = __.newBean('com.project.ServerTimeHelper');
    bean.getTime();

Best regards,
Anatol

Thank you,
we implemented it as rymsha suggested and it works great! :+1:
Iā€™m adding the query for retrieving the time, just for information for others :slight_smile:

{
  ...on Query{
    getServerTime
  }
}